An introduction to ADO.NET concluded - The dataset classes
(Page 3 of 4 )
Figure 2-7 presents some of the properties and methods of the four main classes that you use to work with a dataset: DataSet, DataTable, DataColumn, and DataRow. As you saw in the previous figure, the objects you create from these classes form a hierarchy where each dataset can contain one or more tables and each table can contain one or more rows and one or more columns. Because of that, a dataset contains a Tables property that provides access to the collection of tables in the dataset. Similarly, a data table contains a Columns property and a Rows property that provide access to the collections of columns and rows in the table. These are the properties you’re most likely to use as you work with these objects.
Although they’re not shown in this figure, the collections you refer to through the Tables property of a dataset and the Columns and Rows properties of a data table have properties and methods of their own. For instance, each collection has a Count property that you can use to determine how many items are in the collection. To get the number of tables in a dataset named dsPayables, for example, you could use code like this:
dsPayables.Tables.Count()
To access a specific item in a collection, you use the Item property. On that property, you specify the index value or name of the item you want to access. To access the Vendors table in the dsPayables dataset, for example, you can use code like this:
dsPayables.Tables.Item("Vendors")
Since Item is the default property of the collection class, however, you typically omit it like this:
dsPayables.Tables("Vendors")
The code in this figure shows how you can use a For Each…Next statement to loop through the items in a collection. Here, the statement loops through the rows in the Vendors table. To do that, it uses a variable that’s declared as a DataRow object. Then, the For Each…Next statement uses this variable to retrieve the value of the VendorName column in each row. You can use similar code to loop through the columns in a table or the tables in a dataset.
Figure 2-7. The DataSet, DataTable, DataColumn, and
DataRow classes
Common properties and methods of the DataSet class

Common properties and methods of the DataTable class

Common properties and methods of the DataColumn class

Common properties and methods of the DataRow class

Code that refers to the rows collection in the tables collection of a dataset
Dim sMsg As String, dr As DataRow
For Each dr In dsVendors.Tables("Vendors").Rows
sMsg &= dr.Item("VendorName") & ControlChars.CrLf
Next
MessageBox.Show(sMsg)
Description
- You’ll use the properties and methods of the dataset classes most often when you work with ADO.NET objects through code, as described in chapter 6.
- Each collection of objects has properties and methods that you can use to work with the collection.
Next: Concurrency and the disconnected data architecture >>
More .NET Articles
More By Murach Publishing
|
This article is excerpted from chapter two of the book VB.NET Database: Database Programming with ADO.NET, written by Anne Prince and Doug Lowe (Murach Publishing; ISBN: 1890774197). Check it out today at your favorite bookstore. Buy this book now.
|
|