9.4. Creating an XML File That Shows Changes Made to a DataSet

Problem

When you use the GetXML() method of the DataSet, you may see only the current values in the DataSet. You want to get the original values and see which rows were added, edited, or deleted.

Solution

Create an XML DiffGram, which is a document that details the modifications made to a DataSet.

The solution loads the TOP three rows for the HumanResources.Department table in AdventureWorks into a DataSet and displays the DiffGram for the DataSet before modification. The solution then deletes the first row, modifies the second row, and adds a row to the end of the Department DataTable. The DiffGram for the DataSet after the changes is output to the console.

The C# code in Program.cs in the project CreateXmlDataSetChanges is shown in Example 9-6.

Example 9-6. File: Program.cs for CreateXmlDataSetChanges solution

using System; using System.Data; using System.Data.SqlClient; using System.IO; using System.Text; namespace CreateXmlDataSetChanges { class Program { static void Main(string[] args) { string sqlConnectString = "Data Source=(local);" + "Integrated security=SSPI;Initial Catalog=AdventureWorks;"; string sqlSelect = "SELECT TOP 3 DepartmentID, Name, GroupName, ModifiedDate " + "FROM HumanResources.Department"; SqlDataAdapter da = new SqlDataAdapter(sqlSelect, sqlConnectString); DataSet ds = new DataSet(); da.TableMappings.Add("Table", "Department"); da.FillSchema(ds, SchemaType.Mapped); ds.Tables["Department"].Columns["DepartmentID"].AutoIncrementSeed ...

Get ADO.NET 3.5 Cookbook, 2nd Edition 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.