5.6. Merging Data
Problem
You have two DataSet objects
with the same schema, but containing different data. You need to
combine data from these two DataSet objects
without creating duplicate rows.
Solution
Use the DataSet.Merge( )
method with the appropriate
MissingSchemaAction
enumeration values.
The sample code contains four event handlers and a single method:
Form.LoadSets up the sample by creating two
DataSetobjects each with a singleDataTablecontaining different subset of data from the Employees table in Northwind. The default view for each table is bound to a data grid on the form.- MergeA
Button.Click The first
Button.Clickcalls theMerge( )method to merge the firstDataSetinto the second.- MergeB
Button.Click A second
Button.Clickcalls theMerge( )method to merge the secondDataSetinto the first.- Clear
Button.Click A third
Button.Clickclears the data grid displaying the results of either merge.Merge( )This method takes two
DataTablearguments. The firstDataTableis copied to a newDataTableand the secondDataTableis merged into it with the specifiedMissingSchemaAction. The default view of the resultDataTableis bound to a data grid on the form.
The C# code is shown in Example 5-6.
Example 5-6. File: MergingDataForm.cs
// Namespaces, variables, and constants using System; using System.Configuration; using System.Windows.Forms; using System.Data; using System.Data.SqlClient; private SqlDataAdapter daA, daB; private DataSet dsA, dsB; // . . . private void MergingDataForm_Load(object ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access