6

嗨,我真的不明白数据表和数据视图之间的区别,因为我们可以这样做:

   Dim dtb As New DataTable()
   Dim dtv As DataView = dtb.DefaultView

提前致谢 。

4

3 回答 3

12

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();



 }

当然,创建和维护 DataView 会影响性能,因此您可以选择仅在真正需要时才使用它

于 2013-03-25T13:29:02.327 回答
2

有很多与此相关的互联网链接,但仅供参考

DataView 是 DataTable 的自定义视图,用于排序、筛选、搜索、编辑和导航。DataView 不存储数据,而是表示其对应 DataTable 的连接视图。

您可以查看 VB 中的简单 DataView示例

于 2013-03-25T13:29:48.763 回答
1

DataView 是用于过滤或应用表达式排序等的附加层。

DataView 包含诸如RowFilter

链接:http: //msdn.microsoft.com/fr-fr/library/system.data.dataview.aspx

于 2013-03-25T13:30:07.657 回答