Export (0) Print
Expand All

DataSet.Merge Method

Merges a specified DataSet, DataTable, or array of DataRow objects into the current DataSet or DataTable.

Overload List

Merges an array of DataRow objects into the current DataSet.

[Visual Basic] Overloads Public Sub Merge(DataRow())
[C#] public void Merge(DataRow[]);
[C++] public: void Merge(DataRow*[]);
[JScript] public function Merge(DataRow[]);

Merges a specified DataSet and its schema into the current DataSet.

[Visual Basic] Overloads Public Sub Merge(DataSet)
[C#] public void Merge(DataSet);
[C++] public: void Merge(DataSet*);
[JScript] public function Merge(DataSet);

Merges a specified DataTable and its schema into the current DataSet.

[Visual Basic] Overloads Public Sub Merge(DataTable)
[C#] public void Merge(DataTable);
[C++] public: void Merge(DataTable*);
[JScript] public function Merge(DataTable);

Merges a specified DataSet and its schema into the current DataSet, preserving or discarding any changes in this DataSet according to the given argument.

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

Merges an array of DataRow objects into the current DataSet, preserving or discarding changes in the DataSet and handling an incompatible schema according to the given arguments.

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

Merges a specified DataSet and its schema with the current DataSet, preserving or discarding changes in the current DataSet and handling an incompatible schema according to the given arguments.

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

Merges a specified DataTable and its schema into the current DataSet, preserving or discarding changes in the DataSet and handling an incompatible schema according to the given arguments.

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

Example

[Visual Basic, C#, C++] The following example creates a simple DataSet with one table, two columns, and ten rows. A second DataTable is created that is nearly identical to the first except that a new DataColumn is added to the table. Two rows are added to the second table, which is then merged into the DataSet with the preserveChanges argument set to false, and the missingSchemaAction argument set to MissingSchemaAction.Add.

[Visual Basic, C#, C++] Note   This example shows how to use one of the overloaded versions of Merge. For other examples that might be available, see the individual overload topics.
[Visual Basic] 
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] 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.

See Also

DataSet Class | DataSet Members | System.Data Namespace

Show:
© 2014 Microsoft