Merges a specified System.Data.DataSet and its schema into the current DataSet.
The DataSet.Merge(DataSet) method is used to merge two System.Data.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 System.Data.DataSet. This allows the client application to have a refreshed System.Data.DataSet with the latest data from the data source.
The DataSet.Merge(DataSet) 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 System.Data.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 DataSet.GetChanges method is first invoked. That method returns a second System.Data.DataSet optimized for validating and merging. This second System.Data.DataSet object contains only the System.Data.DataTable and System.Data.DataRow objects that were changed, resulting in a subset of the original System.Data.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 System.Data.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 System.Data.DataSet can be merged back into the client application's original System.Data.DataSet with the DataSet.Merge(DataSet) method.
When the DataSet.Merge(DataSet) method is called, the schemas of the two System.Data.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 System.Data.DataSet contains schema elements (added System.Data.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 System.Data.DataSet contains the added schema and data.
After merging schemas, the data is merged.
When merging a new source System.Data.DataSet into the target, any source rows with a System.Data.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 System.Data.ConstraintException is generated and the merged data is retained while the constraints are disabled. In this case, the DataSet.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 DataSet.EnforceConstraints property to true.