4.7. Updating a Data Source with Data from a Different Data Source
Problem
You want to update a data source using changes made to another data source for data replication or auditing purposes.
Solution
Use the GetChanges( ) method of the
DataSet to identify changes made to a
DataSet and replicate these changes into a
different data source.
The sample code contains two event handlers:
Form.LoadSets up the example by setting up two
DataSetobjects each containing a single table. The first table is filled with the Customers table from Northwind stored in SQL Server; the second is filled with the Customers table from Northwind stored in a MSDE instance. The default view of each table is bound to a data grid on the form.- Update Destination
Button.Click Creates a new
DataSetcontaining only the records that have changed in the original data source. ThisDataSetis then used to apply the changes to a second data source using itsDataAdapter; theDataSetfor the second data source is reloaded. Finally, the first data source is updated with the changes.
The C# code is shown in Example 4-10.
Example 4-10. File: UpdateDataFromDifferentDataSourceForm.cs
// Namespaces, variables, and constants using System; using System.Configuration; using System.Windows.Forms; using System.Data; using System.Data.SqlClient; private DataSet dsSource, dsDest; private SqlDataAdapter daSource, daDest; // . . . private void UpdateDataFromDifferentDataSourceForm_Load(object sender, System.EventArgs e) { // Create the DataAdapter ...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