tailieunhanh - Resolving Data Conflicts

[ Team LiB ] Recipe Resolving Data Conflicts Problem You need to effectively resolve data conflicts and prevent overwriting of existing data when attempting to update changes in a DataSet to a database where the underlying data has changed. | Team LiB Recipe Resolving Data Conflicts Problem You need to effectively resolve data conflicts and prevent overwriting of existing data when attempting to update changes in a DataSet to a database where the underlying data has changed. Solution Handle the DBConcurrencyException within the RowUpdated event of the DataAdapter. The schema of table TBL0611 used in this solution is shown in Table 6-13. Table 6-13. TBL0611 schema Column name Data type Length Allow nulls Id int 4 No Field1 nvarchar 50 Yes The sample code contains seven event handlers Creates a DataSet A containing a single DataTable A filling the schema and data for the table from TBL0611 from the database using a DataAdapter. The ContinueUpdateOnError property of the DataAdapter is set to true. A CommandBuilder object is created to generate the updating logic for the DataAdapter. The default view of DataTable A is bound to a data grid on the form. A conflict table is created to store the original row data for a row when a concurrency error is encountered while updating a row from DataTable A back to TBL0611 in the database. A DataAdapter that uses a parameterized SQL SELECT statement to retrieve the original row data is created. The schema for the conflict table is loaded from TBL0611 using the DataAdapter. The default view of the conflict table is bound to a data grid on the form. A DataSet B and a conflict table for DataTable B row update concurrency errors are created in the same way as described for DataSet A. Refresh A Clears the conflict table clears the data in table A and uses a DataAdapter to fill table A from TBL0611 in the database. Refresh B Clears the conflict table clears the data in table B and uses a DataAdapter to fill table B from TBL0611 in the database. Update A Clears the conflict table and uses a DataAdapter to update changes made to table A back to table TBL0611 in the database. Update B Clears the conflict table