DataSet.Merge Method (DataTable)


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

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

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

public void Merge(
	DataTable table


Type: System.Data.DataTable

The DataTable whose data and schema will be merged.

Exception Condition

The table is null.

The Merge method is used to merge two DataSet 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 DataSet. This allows the client application to have a refreshed DataSet with the latest data from the data source.

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 DataSet.

In a client application, it is common 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 DataSet optimized for validating and merging. This second DataSet object contains only the DataTable and DataRow objects that were changed, resulting in a subset of the original DataSet. 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 DataSet 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 DataSet can be merged back into the client application's original DataSet with the Merge method.

When the Merge method is called, the schemas of the two DataSet 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 DataSet 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 DataSet contains the added schema and data.

After merging schemas, the data is merged.

When merging a new source DataSet 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.

During a merge, constraints are disabled. If any constraints cannot be enabled at the end of merge, a ConstraintException is generated and the merged data is retained while the constraints are disabled. In this case, the EnforceConstraints property is set to false, and all rows that are invalid are marked in error. The errors must be resolved before attempting to reset the EnforceConstraints property to true.

The following example creates a simple DataSet with one table, two columns, and ten rows. A second DataTable is created that is identical to the first. Two rows are added to the second table, which is then merged into the DataSet.

private void DemonstrateMergeTable()
    // Create a DataSet with one table, two columns, and ten rows.
    DataSet dataSet = new DataSet("dataSet");
    DataTable table = new DataTable("Items");

    // Add table to the DataSet

    // Add columns
    DataColumn c1 = new DataColumn("id", 
    DataColumn c2 = new DataColumn("Item", 

    // DataColumn array to set primary key.
    DataColumn[] keyCol= new DataColumn[1];

    // Set primary key column.
    keyCol[0]= c1;

    // Add a RowChanged event handler for the table.
    table.RowChanged += new 

    // Add ten rows.
    for(int i = 0; i <10;i++)
        DataRow row=table.NewRow();
        row["id"] = i;
        row["Item"]= i;
    // Accept changes.

    PrintValues(dataSet, "Original values");

    // Create a second DataTable identical to the first.
    DataTable t2 = table.Clone();

    // Add three rows. Note that the id column can'te be the
    // same as existing rows in the DataSet table.
    DataRow newRow;
    newRow = t2.NewRow();
    newRow["id"] = 14;
    newRow["item"] = 774;

    //Note the alternative method for adding rows.
    t2.Rows.Add(new Object[] { 12, 555 });
    t2.Rows.Add(new Object[] { 13, 665 });

    // Merge the table into the DataSet
    PrintValues(dataSet, "Merged With table.");


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

private void PrintValues(DataSet dataSet, string label)
    Console.WriteLine("\n" + label);
    foreach(DataTable table in dataSet.Tables)
        Console.WriteLine("TableName: " + table.TableName);
        foreach(DataRow row in table.Rows)
            foreach(DataColumn column in table.Columns)
                Console.Write("\table " + row[column] );

.NET Framework
Available since 1.1
Return to top