Ste*_*eve 11
这Datatable是根据您的数据库中的查询提取的无序和未过滤的DataRows集合.
DataView(您可以拥有多个)是相同数据的过滤和/或有序视图.
例如:
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();
}
Run Code Online (Sandbox Code Playgroud)
当然,创建和维护DataView是对性能的一种打击,因此您可以选择仅在您真正需要时才使用它