3.3. Determining the Differences in Data Between Two DataSet Objects
Problem
You have two DataSet objects
with the same schema but containing
different data and need to determine the difference between the data
in the two.
Solution
Compare the two DataSet objects with
the
GetDataSetDifference( )
method shown in this solution and return the differences between the
data as a DiffGram.
The sample code contains two event handlers and a single method:
Form.LoadSets up the sample by creating two
DataSetobjects each containing a different subset of records from the Categories table from the Northwind sample database. The default view for each table is bound to a data grid on the form.- Get Difference
Button.Click Simply calls
GetDataSetDifference( )when the user clicks the button.GetDataSetDifference( )This method takes two
DataSetobjects with identical schemas as arguments and returns aDiffGramof the differences between the data in the two.
The C# code is shown in Example 3-3.
Example 3-3. File: DataSetDifferenceForm.cs
// Namespaces, variables, and constants using System; using System.Configuration; using System.IO; using System.Data; using System.Data.SqlClient; // Field name constants private const String CATEGORYID_FIELD = "CategoryID"; DataSet dsA, dsB; // . . . private void DataSetDifferenceForm_Load(object sender, System.EventArgs e) { SqlDataAdapter da; String sqlText; // Fill table A with Category schema and subset of data. sqlText = "SELECT CategoryID, CategoryName, Description ...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