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

Sets up the sample by creating two DataSet objects 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 DataSet objects with identical schemas as arguments and returns a DiffGram of 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 ...

Get ADO.NET Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.