Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
AddRange Method

DataTableCollection.AddRange Method

Copies the elements of the specified DataTable array to the end of the collection.

[Visual Basic]
Public Sub AddRange( _
   ByVal tables() As DataTable _
)
[C#]
public void AddRange(
 DataTable[] tables
);
[C++]
public: void AddRange(
 DataTable* tables[]
);
[JScript]
public function AddRange(
   tables : DataTable[]
);

Parameters

tables
The array of DataTable objects to add to the collection.

Example

[Visual Basic, C#, C++] The following example creates two DataTable objects and adds them to the DataTableCollection of a DataSet.

[Visual Basic] 
Public Shared Sub DataTableCollectionAddRange()
    ' create a DataSet with two tables
    Dim myDataSet As DataSet = New DataSet()

    ' create Customer table
    Dim t As DataTable = New DataTable( "Customers" )
    t.Columns.Add( "customerId", Type.GetType("System.Int32")).AutoIncrement = True
    t.Columns.Add( "name", Type.GetType("System.String"))
    t.PrimaryKey = New DataColumn() { t.Columns("customerId") }

    ' create Orders table
    Dim t2 As DataTable = New DataTable( "Orders" )
    t2.Columns.Add( "orderId", Type.GetType("System.Int32")).AutoIncrement = True
    t2.Columns.Add( "customerId", Type.GetType("System.Int32"))
    t2.Columns.Add( "amount",     Type.GetType("System.Double"))
    t2.PrimaryKey = New DataColumn() { t.Columns("orderId") }

    myDataSet.Tables.AddRange( New DataTable() {t, t2} )

    ' print the tables and their columns
    Dim table As DataTable
    Dim column As DataColumn
    For Each table In myDataSet.Tables 
        Console.WriteLine( table.TableName )
        For Each column In table.Columns 
            Console.Write( "{0}" & vbTab, column.ColumnName )
        Next
        Console.WriteLine()
    Next
End Sub

[C#] 
public static void DataTableCollectionAddRange()
{
    // create a DataSet with two tables
    DataSet myDataSet = new DataSet();

    // create Customer table
    DataTable t = new DataTable( "Customers" );
    t.Columns.Add( "customerId", typeof(int)    ).AutoIncrement = true;
    t.Columns.Add( "name",       typeof(string) );
    t.PrimaryKey = new DataColumn[] { t.Columns["customerId"] };

    // create Orders table
    DataTable t2 = new DataTable( "Orders" );
    t2.Columns.Add( "orderId",    typeof(int)    ).AutoIncrement = true;
    t2.Columns.Add( "customerId", typeof(int)    );
    t2.Columns.Add( "amount",     typeof(double) );
    t2.PrimaryKey = new DataColumn[] { t.Columns["orderId"] };

    myDataSet.Tables.AddRange( new DataTable[] {t, t2} );

    // print the tables and their columns
    foreach( DataTable table in myDataSet.Tables )
    {
        Console.WriteLine( table.TableName );
        foreach( DataColumn column in table.Columns )
        {
            Console.Write( "{0}\t", column.ColumnName );
        }
        Console.WriteLine();
    }
}

[C++] 
public:
static void DataTableCollectionAddRange()
{
    // create a DataSet with two tables
    DataSet* myDataSet = new DataSet();

    // create Customer table
    DataTable* t = new DataTable(S"Customers");
    t->Columns->Add(S"customerId", __typeof(int))->AutoIncrement = true;
    t->Columns->Add(S"name",       __typeof(String));
    DataColumn* columnArray1[] = { t->Columns->Item[S"customerId"] };
    t->PrimaryKey = columnArray1;

    // create Orders table
    DataTable* t2 = new DataTable(S"Orders");
    t2->Columns->Add(S"orderId",    __typeof(int))->AutoIncrement = true;
    t2->Columns->Add(S"customerId", __typeof(int));
    t2->Columns->Add(S"amount",     __typeof(double));
    DataColumn* columnArray2[] = { t->Columns->Item[S"orderId"] };
    t2->PrimaryKey = columnArray2;
    DataTable* tableArray[] = {t, t2};
    myDataSet->Tables->AddRange(tableArray);

    // print the tables and their columns
    System::Collections::IEnumerator* myEnum = myDataSet->Tables->GetEnumerator();
    while (myEnum->MoveNext()) {
        DataTable* table = __try_cast<DataTable*>(myEnum->Current);
        Console::WriteLine(table->TableName);
        System::Collections::IEnumerator* myEnum = table->Columns->GetEnumerator();
        while (myEnum->MoveNext()) {
            DataColumn* column = __try_cast<DataColumn*>(myEnum->Current);
            Console::Write(S" {0}\t", column->ColumnName);
        }
        Console::WriteLine();
    }
};

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework

See Also

DataTableCollection Class | DataTableCollection Members | System.Data Namespace

Show:
© 2015 Microsoft