DataTable.Merge Method (DataTable, Boolean, MissingSchemaAction)


The .NET API Reference documentation has a new home. Visit the .NET API Browser on to see the new experience.

Merge the specified DataTable with the current DataTable, indicating whether to preserve changes and how to handle missing schema in the current DataTable.

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

public void Merge(
	DataTable table,
	bool preserveChanges,
	MissingSchemaAction missingSchemaAction


Type: System.Data.DataTable

The DataTable to be merged with the current DataTable.

Type: System.Boolean

true, to preserve changes in the current DataTable; otherwise false.

Type: System.Data.MissingSchemaAction

One of the MissingSchemaAction values.

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 during the merge operation unless the developer specifies false for the preserveChanges parameter. If the preserveChanges parameter is set to true, incoming values do not overwrite existing values in the Current row version of the existing row. If the preserveChanges parameter is set to false, incoming values do overwrite the existing values in the Current row version of the existing row. For more information about row versions, see Row States and Row Versions.

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 DataTable and DataRow objects that were changed, resulting in a subset of the original DataTable. This subset is generally smaller, and thus this subset is 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 M:System.Data.DataTable.Merge method.

When the M:System.Data.DataTable.Merge method is called, the schemas of the two DataTable objects are compared, because it is possible that the schemas may have been changed. For example, in a business-to-business scenario, new columns may have been added to an XML schema by an automated process. If the source DataTable contains schema elements (added DataColumn objects) that are missing in the target, the schema elements can be added to the target by setting the missingSchemaAction argument to MissingSchemaAction.Add. In that case, the merged DataTable contains the added schema and data.

After merging schemas, the data is merged.

When merging a new source DataTable into the target, any source rows with a DataRowState value of Unchanged, Modified, or Deleted are 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.

The following console application demonstrates the behavior of the missingSchemaAction parameter of the Merge method. This example creates two versions of the same table, modifying the schema for the second version. The code then attempts to merge the second table into the first.

private static void DemonstrateMergeTable()
    DataTable itemsTable = new DataTable("Items");

    // Add columns
    DataColumn idColumn = new DataColumn("id", typeof(System.Int32));
    DataColumn itemColumn = new DataColumn("item", typeof(System.Int32));

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

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

    // Add ten rows.
    DataRow row;
    for (int i = 0; i <= 9; i++)
        row = itemsTable.NewRow();
        row["id"] = i;
        row["item"] = i;

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

    // Create a second DataTable identical to the first.
    DataTable itemsClone = itemsTable.Clone();

    // Add column to the second column, so that the 
    // schemas no longer match.
    itemsClone.Columns.Add("newColumn", typeof(System.String));

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

    row = itemsClone.NewRow();
    row["id"] = 12;
    row["item"] = 555;
    row["newColumn"] = "new column 2";

    row = itemsClone.NewRow();
    row["id"] = 13;
    row["item"] = 665;
    row["newColumn"] = "new column 3";

    // Merge itemsClone into the itemsTable.
    itemsTable.Merge(itemsClone, false, MissingSchemaAction.Add);
    PrintValues(itemsTable, "Merged With itemsTable, schema added");

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:
    foreach (DataRow row in table.Rows)
        foreach (DataColumn col in table.Columns)
            Console.Write("\t " + row[col].ToString());

.NET Framework
Available since 2.0
Return to top