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( _ ByVal table As DataTable, _ ByVal preserveChanges As Boolean, _ ByVal missingSchemaAction As MissingSchemaAction _ ) [C#] public void Merge( DataTable table, bool preserveChanges, MissingSchemaAction missingSchemaAction ); [C++] public: void Merge( DataTable* table, bool preserveChanges, MissingSchemaAction missingSchemaAction ); [JScript] public function Merge( table : DataTable, preserveChanges : Boolean, missingSchemaAction : MissingSchemaAction );
| Exception Type | Condition |
|---|---|
| ArgumentNullException | The dataSet is a null reference (Nothing in Visual Basic). |
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.
iOn 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 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 will then update 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 will contain 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.
[Visual Basic, 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] 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++, JScript] No example is available for C++ or JScript. To view a Visual Basic or C# example, click the Language Filter button
in the upper-left corner of the page.
Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family
DataSet Class | DataSet Members | System.Data Namespace | DataSet.Merge Overload List