Đang chuẩn bị liên kết để tải về tài liệu:
Updating a Data Source with Data from a Different Data Source
Đang chuẩn bị nút TẢI XUỐNG, xin hãy chờ
Tải xuống
[ Team LiB ] Recipe 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( ) | Team LiB Recipe 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.Load 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 Button.Click 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 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 H . . . private void UpdateDataFromDifferentDataSourceForm_Load object sender System.EventArgs e Create the DataAdapter for the source records. daSource new SqlDataAdapter SELECT FROM Customers ConfigurationSettings.AppSettings S ql_ConnectString SqlCommandBuilder cbSource new SqlCommandBuilder daSource dsSource new DataSet Get the schema and data for the source. daSource.FillSchema dsSource SchemaType.Source Customers daSource.Fill dsSource Customers Bind the default view of the customers table to the grid. dataGridSource.DataSource dsSource.Tables Customers .DefaultView Create the DataAdapter for the .