.NET Framework Class Library
DataTable..::.Merge Method (DataTable)

Merge the specified DataTable with the current DataTable.

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

Visual Basic (Declaration)
Public Sub Merge ( _
    table As DataTable _
)
Visual Basic (Usage)
Dim instance As DataTable
Dim table As DataTable

instance.Merge(table)
C#
public void Merge(
    DataTable table
)
Visual C++
public:
void Merge(
    DataTable^ table
)
JScript
public function Merge(
    table : DataTable
)

Parameters

table
Type: System.Data..::.DataTable
The DataTable to be merged with the current DataTable.
Remarks

The Merge method is used to merge two DataTable objects that have largely similar schemas. A merge is typically used on a client application to incorporate the latest changes from a data source into an existing DataTable. This allows the client application to have a refreshed DataTable with the latest data from the data source.

The merge operation takes into account only the original table, and the table to be merged. Child tables are not affected or included. If a table has one or more child tables, defined as part of a relationship, each child table must be merged individually.

The Merge method is typically called at the end of a series of procedures that involve validating changes, reconciling errors, updating the data source with the changes, and finally refreshing the existing DataTable.

When performing a merge, changes made to the existing data before the merge are preserved by default during the merge operation. Developers can modify this behavior by calling one of the other two overloads for this method, and specifying a false value for the preserveChanges parameter.

In a client application, it is usual to have a single button that the user can click that gathers the changed data and validates it before sending it back to a middle tier component. In this scenario, the GetChanges method is first invoked. That method returns a second DataTable optimized for validating and merging. This second DataTable object contains only the DataRow objects that were changed, resulting in a subset of the original DataTable. This subset is generally smaller and thus more efficiently passed back to a middle tier component. The middle tier component then updates the original data source with the changes through stored procedures. The middle tier can then send back either a new DataTable that includes original data and the latest data from the data source (by running the original query again), or it can send back the subset with any changes that have been made to it from the data source. (For example, if the data source automatically creates unique primary key values, these values can be propagated back to the client application.) In either case, the returned DataTable can be merged back into the client application's original DataTable with the Merge()()() method.

When merging a new source DataTable into the target, any source rows with a DataRowState value of Unchanged, Modified, or Deleted, is matched to target rows with the same primary key values. Source rows with a DataRowState value of Added are matched to new target rows with the same primary key values as the new source rows.

Examples

The following console application creates a simple DataTable and adds data to the table. The example then creates a copy of the table, adding rows to the copy. Finally, the example calls the Merge method to merge the data in the second table with the data in the first table.

Visual Basic
Private Sub DemonstrateMergeTable()
  Dim table1 As New DataTable("Items")

  ' Add columns
  Dim column1 As New DataColumn("id", GetType(System.Int32))
  Dim column2 As New DataColumn("item", GetType(System.Int32))
  table1.Columns.Add(column1)
  table1.Columns.Add(column2)

  ' Set the primary key column.
  table1.PrimaryKey = New DataColumn() {column1}

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

  ' Add some rows.
  Dim row As DataRow
  For i As Integer = 0 To 3
    row = table1.NewRow()
    row("id") = i
    row("item") = i
    table1.Rows.Add(row)
  Next i

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

  ' Create a second DataTable identical to the first.
  Dim table2 As DataTable = table1.Clone()

  ' Add three rows. Note that the id column can't be the 
  ' same as existing rows in the original table.
  row = table2.NewRow()
  row("id") = 14
  row("item") = 774
  table2.Rows.Add(row)

  row = table2.NewRow()
  row("id") = 12
  row("item") = 555
  table2.Rows.Add(row)

  row = table2.NewRow()
  row("id") = 13
  row("item") = 665
  table2.Rows.Add(row)

  ' Merge table2 into the table1.
  Console.WriteLine("Merging")
  table1.Merge(table2)
  PrintValues(table1, "Merged With table1")

End Sub

Private Sub Row_Changed(ByVal sender As Object, _
  ByVal e As DataRowChangeEventArgs)
  Console.WriteLine("Row changed {0}{1}{2}", _
    e.Action, ControlChars.Tab, e.Row.ItemArray(0))
End Sub

Private Sub PrintValues(ByVal table As DataTable, _
  ByVal label As String)
  ' Display the values in the supplied DataTable:
  Console.WriteLine(label)
  For Each row As DataRow In table.Rows
    For Each col As DataColumn In table.Columns
      Console.Write(ControlChars.Tab + " " + row(col).ToString())
    Next col
    Console.WriteLine()
  Next row
End Sub
C#
private static void DemonstrateMergeTable()
{
    DataTable table1 = new DataTable("Items");

    // Add columns
    DataColumn column1 = new DataColumn("id", typeof(System.Int32));
    DataColumn column2 = new DataColumn("item", typeof(System.Int32));
    table1.Columns.Add(column1);
    table1.Columns.Add(column2);

    // Set the primary key column.
    table1.PrimaryKey = new DataColumn[] { column1 };

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

    // Add some rows.
    DataRow row;
    for (int i = 0; i <= 3; i++)
    {
        row = table1.NewRow();
        row["id"] = i;
        row["item"] = i;
        table1.Rows.Add(row);
    }

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

    // Create a second DataTable identical to the first.
    DataTable table2 = table1.Clone();

    // Add three rows. Note that the id column can't be the 
    // same as existing rows in the original table.
    row = table2.NewRow();
    row["id"] = 14;
    row["item"] = 774;
    table2.Rows.Add(row);

    row = table2.NewRow();
    row["id"] = 12;
    row["item"] = 555;
    table2.Rows.Add(row);

    row = table2.NewRow();
    row["id"] = 13;
    row["item"] = 665;
    table2.Rows.Add(row);

    // Merge table2 into the table1.
    Console.WriteLine("Merging");
    table1.Merge(table2);
    PrintValues(table1, "Merged With table1");

}

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

private static void PrintValues(DataTable table, string label)
{
    // Display the values in the supplied DataTable:
    Console.WriteLine(label);
    foreach (DataRow row in table.Rows)
    {
        foreach (DataColumn col in table.Columns)
        {
            Console.Write("\t " + row[col].ToString());
        }
        Console.WriteLine();
    }
}
Platforms

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC, Xbox 360, Zune

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Version Information

.NET Framework

Supported in: 3.5, 3.0, 2.0

.NET Compact Framework

Supported in: 3.5, 2.0

XNA Framework

Supported in: 3.0, 2.0, 1.0
See Also

Reference

Other Resources

Tags :


Community Content

Ahmad Mageed
Merge using Primary Keys for Expected Results

Brief Intro and Solution

A common expectation is that the Merge method will correctly do the following:

  1. Append new columns from the second table to an existing row with the same information (ID) or shared primary key in the first table.
  2. Update new values from the second table to an existing row with a shared primary key in the first table.

After merging the developer finds the result to be incorrect. Instead of correctly merging and maintaining the original number of rows (where a match exists) the merged DataTable ends up adding an entirely new row for each of the merged entries, resulting in duplicate and, possibly, incomplete rows.

Solution: In short the likely reason is that the Primary Key was never specified when the DataTable was setup. Visit the PrimaryKey property page for more info on how to set it up: http://msdn.microsoft.com/en-us/library/system.data.datatable.primarykey.aspx.

Example Output of the Intended and Unintended Behaviors

Consider the following DataTables:

DataTable #1

ID, Customer, OrderID, Price
1, Peter Parker, 321, 200.00
42, Bruce Wayne, 120, 5000.00


DataTable #2

ID, OrderID, Price, Courier
1, 321, 1500.00, UPS
42, 120, 5000.00, DHL

As you can see DT#2 has a new column called "Courier" and no "Customer" column. DT#2 also has a different price set for Peter Parker (1500.00 instead of 200.00). The expected merge result of DT#1 and DT#2 using ID (and maybe also OrderID) as primary keys is shown below:

Expected Merged DataTable

ID, Customer, OrderID, Price, Courier
1, Peter Parker, 321, 1500.00, UPS
42, Bruce Wayne, 120, 5000.00, DHL

The above correctly maintains the number of rows and updated the price for Peter Parker.

The unintended result that occurs when no PrimaryKey is set is shown below:

Unexpected Merged DataTable

ID, Customer, OrderID, Price, Courier
1, Peter Parker, 321, 200.00
42, Bruce Wayne, 120, 5000.00

1, 321, 1500.00, UPS
42, 120, 5000.00, DHL

It is apparent that the above result is incorrect and has resulted in duplication of rows (duplicate IDs) with incomplete information (missing Customer and Courier details).

As mentioned earlier, the solution is to set the PrimaryKey property. Below is code to demonstrate the solution.

Code Example (C#)

The following code demonstrates how to get an expected merged DataTable. Some of the code is redundant for the sake of clarity. I apologize for the clutter; the code formatting disappeared when pasted here.

  
// setup for 1st DataTable
DataTable dt1 = new DataTable();
dt1.Columns.Add("ID", typeof(int));
dt1.Columns.Add("Customer", typeof(string));
dt1.Columns.Add("OrderID", typeof(int));
dt1.Columns.Add("Price", typeof(float));

// set primary key(s) here
dt1.PrimaryKey = new DataColumn[] { dt1.Columns["ID"] };
// Another option: multiple keys
// dt1.PrimaryKey = new DataColumn[] { dt1.Columns["ID"], dt1.Columns["OrderID"] };

// populate with some data
dt1.Rows.Add(new Object[] { 1, "Peter Parker", 321, 200.00 });
dt1.Rows.Add(new Object[] { 42, "Bruce Wayne", 120, 5000.00 });

// setup 2nd DataTable similar to the 1st but w/o a Customer column and with a new Courier column
DataTable dt2 = new DataTable();
dt2.Columns.Add("ID", typeof(int));
dt2.Columns.Add("OrderID", typeof(int));
dt2.Columns.Add("Price", typeof(float));
dt2.Columns.Add("Courier", typeof(string));

// set primary key(s) here
// The merge will work provided one of the DataTables have primary keys set, so this step is optional but consistent
// dt2.PrimaryKey = new DataColumn[] { dt2.Columns["ID"] };
// dt2.PrimaryKey = new DataColumn[] { dt2.Columns["ID"], dt2.Columns["OrderID"] };

// populate with some data, same IDs & OrderIDs as before
dt2.Rows.Add(new Object[] { 1, 321, 1500.00, "UPS" }) ; // new price for P. Parker
dt2.Rows.Add(new Object[] { 42, 120, 5000.00, "DHL" }); // same price for B. Wayne

// show the DataTables
ShowDataTable(dt1, "Showing DataTable 1");
ShowDataTable(dt2, "Showing DataTable 2");
// merge and show
dt1.Merge(dt2);
ShowDataTable(dt1, "Showing Merged DataTable");

ShowDataTable Code

// Simple routine to display the DataTable
public static void ShowDataTable(DataTable dt, string caption)
{
Console.WriteLine(caption);

// show column names
foreach (DataColumn col in dt.Columns)
Console.Write("\t" + col.ColumnName);
Console.WriteLine();
// show values
foreach (DataRow row in dt.Rows)
{
foreach (DataColumn col in dt.Columns)
Console.Write("\t" + row[col]);
Console.WriteLine();
}
Console.WriteLine();
}

Closing Comments

To see the unexpected result using the above code simply comment out all the lines where the PrimaryKey property is set.

Tags :

Page view tracker