Name

Tables

Synopsis


DataTableCollection dtc = DataSet.Tables;

Accesses the DataTableCollection contained by the DataSet that contains the DataTable objects belonging to the DataSet.

Example

The DataTableCollection has two methods that can add a table to a DataSet. The Add( ) method takes an optional table name argument and is used to add tables to the DataTableCollection. If this argument isn’t supplied, the tables are automatically named Table, Table1, Table2, and so on. The following example adds a table to a DataSet:

DataSet ds = new DataSet("MyDataSet");

DataTable dt = new DataTable("MyTable");



// ... code to define the schema for the newly constructed DataTable



ds.Tables.Add(dt);

Alternatively, a DataTable can first be constructed within the DataTablesCollection and subsequently have its schema defined:

DataSet ds = new DataSet("MyDataSet");

DataTable dt = ds.Tables.Add("MyTable");



// ... code to define the schema for newly constructed DataTable

A reference to the table that already exists within the DataSet can be retrieved. Most commonly, this is done using the table name or the table ordinal as shown in these examples:

// using the table name

DataTable dt = ds.Tables("MyTable");



// using the table ordinal

DataTable dt = ds.Tables[0];

The Count property returns the number of tables in the DataSet, as shown here:

Int32 tableCount = ds.Tables.Count;

The Contains( ) method returns whether a specific DataTable exists within a DataSet, as shown next:

bool tableExists = ds.Tables.Contains("MyTable");

Existing tables within the DataSet can be accessed by an indexer, which most commonly is passed the table name or the position of the table within the DataTableCollection as an argument as shown in these examples:

// using the table name

DataTable dt = ds.Tables["MyTable"];



// access the first table in the collection using the table ordinal

DataTable dt = ds.Tables[0];

The IndexOf( ) method allows the index of the table within the collection to be retrieved using either a reference to the table object or the table name. The following example demonstrates both techniques:

// get the index using the name of the table

Int32 tableIndex =  ds.Tables.IndexOf("MyTable");



// get the index using a reference to a table

DataTable dt = ds.Tables.Add("MyTable");



// ... build the table and do some work



// get the index of the table based on the table reference

Int32 tableIndex = ds.Tables.IndexOf(dt);

The DataTableCollection provides a number of other useful methods and properties. The AddRange( ) method allows more than one table to be added to the DataSet at the same time. The method takes an array of DataTable objects as the argument as the following examples show:

// create two new tables

DataTable dt1 = new DataTable();

DataTable dt2 = new DataTable();



// use the AddRange method to add them to the DataSet.

ds.Tables.AddRange(new DataTable[] {dt1, dt2});

There are also two methods that remove tables from a DataSet: the Remove( ) and RemoveAt( ) method. The Remove( ) method takes either the table name or a reference to the table to be removed as an argument as shown in the following example:

DataTable dt = ds.Tables.Add("MyTable");



// remove by table reference

ds.Tables.Remove(dt);



// remove using the table name

ds.Tables.Remove("MyTable");

The RemoveAt( ) method removes the table using the index of the table in the DataSetCollection of the DataSet as shown in the following example:

// removes the first table from the tables collection in the DataSet

ds.Tables.RemoveAt(0);

If you need to remove all tables from a DataSet, the Clear( ) method can be used. It takes no arguments and can be used as shown next:

ds.Tables.Clear();