更新时间:2023-10-19 20:41:34
的数据表
是根据从数据库查询中提取数据行的无序和未经过滤的集合。
该数据视图(你可以有多个)是同一数据的过滤和/或有序视图。
The Datatable
is the unordered and unfiltered collection of DataRows extracted according to your query from your database.
The DataView (and you could have more than one) is a filtered and/or ordered view of the same data.
例如:
using(SqlConnection cn = GetConnection())
{
cn.Open();
SqlDataAdapter da = new SqlDataAdapter("SELECT * FROM Customers", cn);
DataTable dt = new DataTable();
da.Fill(dt);
// At this point dt is filled with datarows extracted from the database in no particular order
// And the DefaultView presents the same record organization (or lack of), but...
// Order on the default view by CustomerName
dt.DefaultView.Sort = "CustomerName";
foreach(DataRowView rv in dt.DefaultView)
Console.WriteLine(rv["CustomerName"].ToString();
// A new dataview with only a certain kind of customers ordered by name
DataView dvSelectedCust = new DataView(dt, "CreditLevel = 1", "CustomerName", DataViewRowState.Unchanged);
foreach(DataRowView rv in dvSelectedCust)
Console.WriteLine(rv["CustomerName"],ToString();
}
当然,创建和维护一个数据视图是一个性能命中,因此你可以选择使用它,只有当你真的需要它
Of course creating and maintaining a DataView is an hit on performances and thus you have the choice to use it only when you really need it