System.Data Namespace
Represents the collection of tables in a DataSet.
The DataTableCollection contains all of the DataTable objects for a given DataSet. This collection is accessed via the DataSet.Tables property.
Like most data collections in ADO.NET, the DataTableCollection uses standard collection methods to manage the items in the collection. These include the methods Add, Clear, and Remove.
The collection also includes a Count property to determine how many DataTable objects are in the collection, and a Contains method to verify whether a particular table is in the collection.
To navigate from one table to another, use the ChildRelations or ParentRelations properties of the DataTable to access the table's collection of DataRelation objects. You can also navigate through the parent/child relationships of the tables in a given DataSet using the DataSet.Relations property.
The first example below retrieves the DataTableCollection of a DataSet and prints the value of each column, in each row, of each table. The second example initializes a new DataTable with two columns, and adds it to the DataTableCollection.
private void GetTables ( DataSet ds ) {
// get each DataTable in the DataTableCollection and print each row value.
foreach ( DataTable tbl in ds.Tables ) {
foreach ( DataRow row in tbl.Rows ) {
foreach ( DataColumn col in tbl.Columns ) {
if ( row [ col ] != null ) {
Response.Write ( row [ col ] );
}
}
}
}
}
private void CreateTable ( DataSet ds ) {
DataTable newTable = new DataTable ( "myTable" );
newTable.Columns.Add ( "ID", typeof ( int ) );
newTable.Columns.Add ( "Name", typeof ( string ) );
ds.Tables.Add ( newTable );
}
Private Sub GetTables ( ds As DataSet )
' get each DataTable in the DataTableCollection and print each row value.
Dim tbl As DataTable
For Each tbl In ds.Tables
Dim row As DataRow
For Each row In tbl.Rows
Dim col As DataColumn
For Each col in tbl.Columns
If Not ( row ( col ) Is Nothing ) Then
Response.Write ( row ( col ) )
End If
Next
Next
Next
End Sub
Private Sub CreateTable ( ds As DataSet )
Dim newTable As new DataTable ( "myTable" )
newTable.Columns.Add ( "ID", Type.GetType ( "System.Int32" ) )
newTable.Columns.Add ( "Name", Type.GetType ( "System.String" ) )
ds.Tables.Add ( newTable )
End Sub |
|
C# |
VB |
DataColumn DataRow DataTable DataSet