DataTableCollection.AddRange Method (DataTable())

 
System_CAPS_noteNote

The .NET API Reference documentation has a new home. Visit the .NET API Browser on docs.microsoft.com to see the new experience.

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

Namespace:   System.Data
Assembly:  System.Data (in System.Data.dll)

Public Sub AddRange (
	tables As DataTable()
)

Parameters

tables
Type: System.Data.DataTable()

The array of DataTable objects to add to the collection.

The following example creates two DataTable objects and adds them to the DataTableCollection of a DataSet.

Public Sub DataTableCollectionAddRange()
    ' create a DataSet with two tables
    Dim dataSet As DataSet = New DataSet()

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

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

    dataSet.Tables.AddRange(New DataTable() {customersTable, ordersTable})

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

.NET Framework
Available since 1.1
Return to top
Show: