Questions That Should Be Asked More Frequently

What Is a DataView Object?

The DataTable object's Select method is powerful and flexible, but it's not always the best solution. It has two major limitations. First, because it accepts such dynamic search criteria, it's not terribly efficient. Also, neither Windows nor Web forms support binding to the Select method's return value—an array of DataRow objects. The ADO.NET solution to both limitations is the DataView object.

The ADO.NET DataTable object is roughly equivalent to a table in a database, so you might assume that the DataView object is similar to a view in a database. While there are some similarities between DataView objects and views in a database, they are not as closely related as DataTable objects and tables in a database are.

DataView Objects Return Data from a DataTable

The DataView object does not maintain its own copy of data. When you access data through a DataView, the DataView returns data stored in the corresponding DataTable.

Views in a database behave the same way. When you query a view, the database returns data from the table or tables referenced in the view.

DataView Objects Are Not SQL Queries

A view in a database is really just a query. When you create a view in a database, you supply the query that the database will execute in order to return data for that view:

CREATE VIEW ViewCustomersAndOrders AS     SELECT C.CustomerID, C.CompanyName, C.ContactName, C.Phone,            O.OrderID, O.EmployeeID, O.OrderDate     FROM Customers C, Orders O WHERE C.CustomerID = O.CustomerID

ADO.NET DataView objects let you filter, sort, and search the contents of DataTable objects, but they are not SQL queries. You cannot use a DataView to join data between two DataTable objects, nor can you use a DataView to view only certain columns in a DataTable. DataView objects do support filtering rows based on dynamic criteria, but they can access only a single DataTable, and all columns in the DataTable are available through the DataView.

Simulating joins using a DataRelation

You can use a DataRelation and an expression-based column to simulate a join. For example, if you have DataTable objects for customer and order information, you can create a relationship between the two DataTable objects and then add an expression-based DataColumn in the orders DataTable to display a column from the customers DataTable:

Visual Basic .NET

ds.Relations.Add("CustomersOrders", _                  ds.Tables("Customers").Columns("CustomerID"), _                  ds.Tables("Orders").Columns("CustomerID")) ds.Tables("Orders").Columns.Add("CompanyName", GetType(String), _                            "Parent(CustomersOrders).CompanyName")

Visual C# .NET

ds.Relations.Add("CustomersOrders",                   ds.Tables["Customers"].Columns["CustomerID"],                   ds.Tables["Orders"].Columns["CustomerID"]); ds.Tables["Orders"].Columns.Add("CompanyName", typeof(string),                          "Parent(CustomersOrders).CompanyName");



Microsoft ADO. NET Core Reference
Microsoft ADO.NET (Core Reference) (PRO-Developer)
ISBN: 0735614237
EAN: 2147483647
Year: 2002
Pages: 104
Authors: David Sceppa

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net