gpt4 book ai didi

c# - 将数据从数据库返回到 View ?

转载 作者:行者123 更新时间:2023-11-29 13:07:20 25 4
gpt4 key购买 nike

我的项目中有一个类,我从数据库中读取所有数据,但这是返回它们的最佳方法吗?

我为此目的使用了数据表。

DataTable entries= new DataTable();

try
{
sql_connection.Open();

MySqlDataAdapter adapter = new MySqlDataAdapter("Select * from test", sql_connection);

adapter.Fill(entries);
}
catch(Exception ex) {}

在我的 View 中,我将它们添加到我的 ListView 中:

r 是我的结果类,只是为了让您知道。

for (int i = 0; i < r.entries.Rows.Count; i++)
{
ListViewItem data = new ListViewItem(r.entries.Rows[i][0].ToString());
data.SubItems.Add(r.entries.Rows[i][1].ToString());
data.SubItems.Add(r.entries.Rows[i][2].ToString());
listView_d.Items.Add(data);
}

这是一个好方法还是有更好的方法?

我的 Form 类不知道 System.Data 或 MySQL 命名空间。

最佳答案

There should always be an abstraction between 3 layers such as Presentation (UI), Middle (Business) layer and Data (Access) layer.

During a 'GET' from data base, the return type should be a database entity say X. Now, your middle layer convert 'x' into Business object (say 'B') and finally the code behind does convert it to UI object (say 'U').


If you don't have a business layer, create a business object(if its Employee data, then create a class named 'Employee') and return the instance to code behind.

It is always a well-appreciated practice.

关于c# - 将数据从数据库返回到 View ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22528505/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com