6.10. Checking for Concurrency Violations
Problem
You need to check for concurrency violations.
Solution
Use a timestamp column to manage data concurrency
violations.
The schema of table TBL0610 used in this solution is shown in Table 6-12.
Table 6-12. TBL0610 schema
|
Column name |
Data type |
Length |
Allow nulls? |
|---|---|---|---|
|
Id |
|
4 |
No |
|
Field1 |
|
50 |
Yes |
|
rowversion |
|
8 |
No |
The sample code contains four event handlers and two methods:
Form.LoadSets up the sample by creating two
DataTableobjects:AandB. For each, aDataAdapteris created and a handler is attached to handle theRowUpdatedevent. TheDataAdapteris used to fill theDataTablewith both schema and data from table TBL0610 in the database. Theupdatecommand is defined for theDataAdapterusing thetimestampcolumn in theWHEREclause to catch concurrency errors. The first row from both tables is displayed on the form simulating the view of two different simultaneous data users.DataAdapter.RowUpdatedChecks if the row was inserted or updated. For those rows, the current value of the timestamp column is retrieved from the TBL0610 in the database and used to update the row in the
DataTable.Update( )This method copies the specified value for Field1 from the text box into the
DataTable. TheDataAdapteris used to updateDataTablechanges to table TBL0610 in the database. If aDBConcurrencyExceptionis encountered, the change to the row is rejected.DisplayRow( )This method is used to display data from a specified row ...
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