Share via


DataTableCollection.AddRange 方法

将指定的 DataTable 数组的元素复制到集合末尾。

**命名空间:**System.Data
**程序集:**System.Data(在 system.data.dll 中)

语法

声明
Public Sub AddRange ( _
    tables As DataTable() _
)
用法
Dim instance As DataTableCollection
Dim tables As DataTable()

instance.AddRange(tables)
public void AddRange (
    DataTable[] tables
)
public:
void AddRange (
    array<DataTable^>^ tables
)
public void AddRange (
    DataTable[] tables
)
public function AddRange (
    tables : DataTable[]
)

参数

  • tables
    要添加到集合中的 DataTable 对象的数组。

示例

下面的示例创建两个 DataTable 对象,并将它们添加到 DataSetDataTableCollection 中。

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
public static void DataTableCollectionAddRange()
{
    // create a DataSet with two tables
    DataSet dataSet = new DataSet();

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

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

    dataSet.Tables.AddRange(new DataTable[] { customersTable, ordersTable });

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

平台

Windows 98、Windows 2000 SP4、Windows CE、Windows Millennium Edition、Windows Mobile for Pocket PC、Windows Mobile for Smartphone、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition

.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

版本信息

.NET Framework

受以下版本支持:2.0、1.1、1.0

.NET Compact Framework

受以下版本支持:2.0、1.0

请参见

参考

DataTableCollection 类
DataTableCollection 成员
System.Data 命名空间