我的项目中有一个类,我从数据库中读取所有数据,但这是返回它们的最佳方法吗?
我为此目的使用了数据表。
DataTable entries= new DataTable();
try
{
sql_connection.Open();
MySqlDataAdapter adapter = new MySqlDataAdapter("Select * from test", sql_connection);
adapter.Fill(entries);
}
catch(Exception ex) {}
在我的视图中我将它们添加到我的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命名空间。
答案 0 :(得分:1)
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.
答案 1 :(得分:0)
理想情况下,您应该返回映射您身份的自定义特殊对象。