Using the DataTable and DataView ClassesThe DataTable class in ADO.NET represents a single database table. The DataView class represents a navigable view of data. It is still possible to send a heterogeneous query to a database and display the results in one table or view, but it is preferable to request tables individually in ADO.NET and express the interrelationships as a DataRelation object. As mentioned earlier, the latter technique yields a DataSet object that can be updated by writing changes to each independent table but displayed as if it contained a single set of heterogeneous data. Having complex master “detail relationships that are updatable is the best of both worlds . We can use code similar to that in Listing 11.3 to demonstrate how to fill a DataTable . When we have a DataTable , we can request a DataView directly. We also have the option of populating the DataView directly. Listing 11.5 demonstrates how to populate a DataTable , and the next subsection demonstrates how to express master “detail relationships in a DataSet . Listing 11.5 Populating a DataTablePrivate Sub InitializeGridWithTable() Dim Connection As OleDbConnection = _ New OleDbConnection(Database.ConnectionString) Dim Adapter As OleDbDataAdapter = _ New OleDbDataAdapter("SELECT * FROM CUSTOMERS", Connection) Dim Customers As DataTable = New DataTable("Customers") Adapter.Fill(Customers) DataGrid1.DataSource = Customers DataGrid1.DataBind() End Sub To initialize the DataTable I copied and pasted the code from Listing 11.3 to create Listing 11.5, substituting a DataSet everywhere with a DataTable . When you have a DataTable you can request a DataView . The DataTable.DefaultView property will return the default DataView that represents the DataTable . You also have the option of creating objects like DataSet , DataTable , and DataView programmatically, designing everything, and populating those ADO.NET elements from some source other than a database. In addition to ADO.NET working at a fundamental level with XML, ADO.NET uses data from providers, and a provider does not have to be a traditional database. |