次の方法で共有


DataTableCollection.AddRange メソッド

指定した DataTable 配列の要素をコレクションの末尾にコピーします。

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[]);

パラメータ

  • tables
    コレクションに追加する DataTable オブジェクトの配列。

使用例

[Visual Basic, C#, C++] 2 つの DataTable オブジェクトを作成し、これらのオブジェクトを DataSetDataTableCollection に追加する例を次に示します。

 
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] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン 言語のフィルタ をクリックします。

必要条件

プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ, .NET Compact Framework - Windows CE .NET

参照

DataTableCollection クラス | DataTableCollection メンバ | System.Data 名前空間