次の方法で共有


DataSet.Merge メソッド

指定した DataSetDataTable 、または DataRow オブジェクトの配列を現在の DataSet または DataTable にマージします。

オーバーロードの一覧

DataRow オブジェクトの配列を現在の DataSet にマージします。

[Visual Basic] Overloads Public Sub Merge(DataRow())

[C#] public void Merge(DataRow[]);

[C++] public: void Merge(DataRow*[]);

[JScript] public function Merge(DataRow[]);

指定した DataSet およびそのスキーマを現在の DataSet にマージします。

[Visual Basic] Overloads Public Sub Merge(DataSet)

[C#] public void Merge(DataSet);

[C++] public: void Merge(DataSet*);

[JScript] public function Merge(DataSet);

指定した DataTable およびそのスキーマを現在の DataSet にマージします。

[Visual Basic] Overloads Public Sub Merge(DataTable)

[C#] public void Merge(DataTable);

[C++] public: void Merge(DataTable*);

[JScript] public function Merge(DataTable);

指定した DataSet およびそのスキーマを現在の DataSet にマージします。この DataSet に対して行われた変更は、指定した引数に従って保持または破棄します。

[Visual Basic] Overloads Public Sub Merge(DataSet, Boolean)

[C#] public void Merge(DataSet, bool);

[C++] public: void Merge(DataSet*, bool);

[JScript] public function Merge(DataSet, Boolean);

DataRow オブジェクトの配列を現在の DataSet にマージします。指定した引数に従って、この DataSet に行われた変更を保持または破棄し、互換性のないスキーマを処理します。

[Visual Basic] Overloads Public Sub Merge(DataRow(), Boolean, MissingSchemaAction)

[C#] public void Merge(DataRow[], bool, MissingSchemaAction);

[C++] public: void Merge(DataRow*[], bool, MissingSchemaAction);

[JScript] public function Merge(DataRow[], Boolean, MissingSchemaAction);

指定した DataSet およびそのスキーマを現在の DataSet にマージします。指定した引数に従って、この DataSet に行われた変更を保持または破棄し、互換性のないスキーマを処理します。

[Visual Basic] Overloads Public Sub Merge(DataSet, Boolean, MissingSchemaAction)

[C#] public void Merge(DataSet, bool, MissingSchemaAction);

[C++] public: void Merge(DataSet*, bool, MissingSchemaAction);

[JScript] public function Merge(DataSet, Boolean, MissingSchemaAction);

指定した DataTable およびそのスキーマを現在の DataSet にマージします。指定した引数に従って、この DataSet に行われた変更を保持または破棄し、互換性のないスキーマを処理します。

[Visual Basic] Overloads Public Sub Merge(DataTable, Boolean, MissingSchemaAction)

[C#] public void Merge(DataTable, bool, MissingSchemaAction);

[C++] public: void Merge(DataTable*, bool, MissingSchemaAction);

[JScript] public function Merge(DataTable, Boolean, MissingSchemaAction);

使用例

[Visual Basic, C#, C++] 1 つのテーブル、2 列、および 10 行で単純な DataSet を作成する例を次に示します。最初の DataTable に似ているが新しい DataColumn が追加される点で異なる、2 つ目のテーブルを作成します。2 つ目のテーブルに 2 つの行を追加した後、引数 preserveChangesfalse 、引数 missingSchemaActionMissingSchemaAction.Add に設定した DataSet にこのテーブルをマージします。

[Visual Basic, C#, C++] メモ   ここでは、Merge のオーバーロード形式のうちの 1 つだけについて、使用例を示します。その他の例については、各オーバーロード形式のトピックを参照してください。

 
Private Sub DemonstrateMergeTableAddSchema()
    ' Create a DataSet with one table, two columns, and ten rows.
    Dim ds As New DataSet("myDataSet")
    Dim t As New DataTable("Items")

    ' Add tables to the DataSet
    ds.Tables.Add(t)

    ' Create and add two columns to the DataTable
    Dim c1 As New DataColumn("id", Type.GetType("System.Int32"), "")
    c1.AutoIncrement = True
    Dim c2 As New DataColumn("Item", Type.GetType("System.Int32"), "")
    t.Columns.Add(c1)
    t.Columns.Add(c2)

    ' DataColumn array to set primary key.
    Dim keyCol(1) As DataColumn

    ' Set primary key column.
    keyCol(0) = c1
    t.PrimaryKey = keyCol

    ' Add RowChanged event handler for the table.
    AddHandler t.RowChanged, AddressOf Row_Changed

    ' Add ten rows.
    Dim i As Integer
    Dim r As DataRow

    For i = 0 To 9
        r = t.NewRow()
        r("Item") = i
        t.Rows.Add(r)
    Next i

    ' Accept changes.
    ds.AcceptChanges()
    PrintValues(ds, "Original values")

    ' Create a second DataTable identical to the first
    ' with one extra column using the Clone method.
    Dim t2 As New DataTable
    t2 = t.Clone()

    ' Add column.
    t2.Columns.Add("extra", Type.GetType("System.String"))

    ' Add two rows. Note that the id column can't be the 
    ' same as existing rows in the DataSet table.
    Dim newRow As DataRow
    newRow = t2.NewRow()
    newRow("id") = 12
    newRow("Item") = 555
    newRow("extra") = "extra Column 1"
    t2.Rows.Add(newRow)
    
    newRow = t2.NewRow()
    newRow("id") = 13
    newRow("Item") = 665
    newRow("extra") = "extra Column 2"
    t2.Rows.Add(newRow)

    ' Merge the table into the DataSet.
    Console.WriteLine("Merging")
    ds.Merge(t2, False, MissingSchemaAction.Add)
    PrintValues(ds, "Merged With Table, Schema Added")
End Sub
 
Private Sub Row_Changed(sender As Object, e As DataRowChangeEventArgs)
    Console.WriteLine("Row Changed " + e.Action.ToString() _
       + ControlChars.Tab + e.Row.ItemArray(0).ToString())
End Sub
   
Private Sub PrintValues(ds As DataSet, label As String)
    Console.WriteLine(ControlChars.Cr + label)
    Dim t As DataTable
    Dim r As DataRow
    Dim c As DataColumn
    For Each t In  ds.Tables
        Console.WriteLine("TableName: " + t.TableName)
        For Each r In  t.Rows             
            For Each c In  t.Columns
                Console.Write(ControlChars.Tab + " " + r(c).ToString())
            Next c
            Console.WriteLine()
        Next r
    Next t
 End Sub

[C#] 
private void DemonstrateMergeTableAddSchema(){
   // Create a DataSet with one table, two columns, and ten rows.
   DataSet ds = new DataSet("myDataSet");
   DataTable t = new DataTable("Items");

   // Add table to the DataSet
   ds.Tables.Add(t);

   // Create and add two columns to the DataTable
   DataColumn c1 = new DataColumn("id", Type.GetType("System.Int32"),"");
   c1.AutoIncrement=true;
   DataColumn c2 = new DataColumn("Item", Type.GetType("System.Int32"),"");
   t.Columns.Add(c1);
   t.Columns.Add(c2);

   // Set the primary key to the first column.
   t.PrimaryKey = new DataColumn[1]{ c1 };

   // Add RowChanged event handler for the table.
   t.RowChanged+= new DataRowChangeEventHandler(Row_Changed);

   // Add ten rows.
   for(int i = 0; i <10;i++){
      DataRow r=t.NewRow();
      r["Item"]= i;
      t.Rows.Add(r);
   }

   // Accept changes.
   ds.AcceptChanges();
   PrintValues(ds, "Original values");

   // Create a second DataTable identical to the first, with
   // one extra column using the Clone method.
   DataTable t2 = t.Clone();
   t2.Columns.Add("extra", typeof(string));

   // Add two rows. Note that the id column can't be the 
   // same as existing rows in the DataSet table.
   DataRow newRow;
   newRow=t2.NewRow();
   newRow["id"]= 12;
   newRow["Item"]=555;
   newRow["extra"]= "extra Column 1";
   t2.Rows.Add(newRow);

   newRow=t2.NewRow();
   newRow["id"]= 13;
   newRow["Item"]=665;
   newRow["extra"]= "extra Column 2";
   t2.Rows.Add(newRow);

   // Merge the table into the DataSet.
   Console.WriteLine("Merging");
   ds.Merge(t2,false,MissingSchemaAction.Add);
   PrintValues(ds, "Merged With Table, Schema Added");
}

private void Row_Changed(object sender, DataRowChangeEventArgs e){
   Console.WriteLine("Row Changed " + e.Action.ToString() + "\t" + e.Row.ItemArray[0]);
}

private void PrintValues(DataSet ds, string label){
   Console.WriteLine("\n" + label);
   foreach(DataTable t in ds.Tables){
      Console.WriteLine("TableName: " + t.TableName);
      foreach(DataRow r in t.Rows){
         foreach(DataColumn c in t.Columns){
            Console.Write("\t " + r[c] );
         }
         Console.WriteLine();
      }
   }
}

[C++] 
private:
 void DemonstrateMergeTableAddSchema(){
    // Create a DataSet with one table, two columns, and ten rows.
    DataSet* ds = new DataSet(S"myDataSet");
    DataTable* t = new DataTable(S"Items");

    // Add table to the DataSet
    ds->Tables->Add(t);

    // Create and add two columns to the DataTable
    DataColumn* c1 = new DataColumn(S"id", Type::GetType(S"System.Int32"),S"");
    c1->AutoIncrement=true;
    DataColumn* c2 = new DataColumn(S"Item", Type::GetType(S"System.Int32"),S"");
    t->Columns->Add(c1);
    t->Columns->Add(c2);

    // Set the primary key to the first column.
    DataColumn* temp[] = { c1 };
    t->PrimaryKey = temp;

    // Add RowChanged event handler for the table.
    t->RowChanged+= new DataRowChangeEventHandler(this, &Form1::Row_Changed);

    // Add ten rows.
    for(int i = 0; i <10;i++){
       DataRow* r=t->NewRow();
       r->Item[S"Item"]= __box(i);
       t->Rows->Add(r);
    }

    // Accept changes.
    ds->AcceptChanges();
    PrintValues(ds, S"Original values");

    // Create a second DataTable identical to the first, with
    // one extra column using the Clone method.
    DataTable* t2 = t->Clone();
    t2->Columns->Add(S"extra", __typeof(String));

    // Add two rows. Note that the id column can't be the 
    // same as existing rows in the DataSet table.
    DataRow* newRow;
    newRow=t2->NewRow();
    newRow->Item[S"id"]= __box(12);
    newRow->Item[S"Item"]=__box(555);
    newRow->Item[S"extra"]= S"extra Column 1";
    t2->Rows->Add(newRow);
 
    newRow=t2->NewRow();
    newRow->Item[S"id"]= __box(13);
    newRow->Item[S"Item"]=__box(665);
    newRow->Item[S"extra"]= S"extra Column 2";
    t2->Rows->Add(newRow);

    // Merge the table into the DataSet.
    Console::WriteLine(S"Merging");
    ds->Merge(t2,false,MissingSchemaAction::Add);
    PrintValues(ds, S"Merged With Table, Schema Added");
 }
 
 void Row_Changed(Object* /*sender*/, DataRowChangeEventArgs* e){
    Console::WriteLine(S"Row Changed {0}\t{1}", __box(e->Action), e->Row->ItemArray[0]);
 }
 
 void PrintValues(DataSet* ds, String* label){
    Console::WriteLine(S"\n{0}", label);
    System::Collections::IEnumerator* myEnum = ds->Tables->GetEnumerator();
    while (myEnum->MoveNext())
    {
       DataTable* t = __try_cast<DataTable*>(myEnum->Current);
       Console::WriteLine(S"TableName: {0}", t->TableName);
       System::Collections::IEnumerator* myEnum1 = t->Rows->GetEnumerator();
       while (myEnum1->MoveNext())
       {
          DataRow* r = __try_cast<DataRow*>(myEnum1->Current);
          System::Collections::IEnumerator* myEnum2 = t->Columns->GetEnumerator();
          while (myEnum2->MoveNext())
          {
             DataColumn* c = __try_cast<DataColumn*>(myEnum2->Current);
             Console::Write(S"\t {0}", r->Item[c] );
          }
          Console::WriteLine();
       }
    }
 }

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

参照

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