tailieunhanh - Updating a Data Source with Data from a Different Data Source

[ Team LiB ] Recipe 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( ) | Team LiB Recipe 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 Sets up the example by setting up two DataSet objects 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 Creates a new DataSet containing only the records that have changed in the original data source. This DataSet is then used to apply the changes to a second data source using its DataAdapter the DataSet for 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 Namespaces variables and constants using System using using using using private DataSet dsSource dsDest private SqlDataAdapter daSource daDest H . . . private void UpdateDataFromDifferentDataSourceForm_Load object sender e Create the DataAdapter for the source records. daSource new SqlDataAdapter SELECT FROM Customers S ql_ConnectString SqlCommandBuilder cbSource new SqlCommandBuilder daSource dsSource new DataSet Get the schema and data for the source. dsSource Customers dsSource Customers Bind the default view of the customers table to the grid. Customers .DefaultView Create the DataAdapter for the .