4.1. 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 in this solution and return the differences between the data as a DiffGram.
The solution creates two DataSet objects each containing a different subset of records from the HumanResources.Department table in AdventureWorks. The GetDataSetDifference() 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 in Program.cs in the project DetermineDataDifferenceDataSets is shown in Example 4-1.
Example 4-1. File: Program.cs for DetermineDataDifferenceDataSets solution
using System; using System.Data; using System.Data.SqlClient; using System.IO; namespace DetermineDataDifferenceDataSets { class Program { static void Main(string[] args) { string sqlConnectString = "Data Source=(local);" + "Integrated security=SSPI;Initial Catalog=AdventureWorks;"; // Fill DataSet A string sqlSelectA = "SELECT * FROM HumanResources.Department " + "WHERE DepartmentID BETWEEN 1 AND 5"; DataSet dsA = new DataSet(); SqlDataAdapter da = new SqlDataAdapter(sqlSelectA, sqlConnectString); da.TableMappings.Add("Table", "TableA"); da.FillSchema(dsA, SchemaType.Source); da.Fill(dsA); // Set the primary key dsA.Tables["TableA"].PrimaryKey ...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