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.Load
Sets up the sample by creating two
DataTable
objects:A
andB
. For each, aDataAdapter
is created and a handler is attached to handle theRowUpdated
event. TheDataAdapter
is used to fill theDataTable
with both schema and data from table TBL0610 in the database. Theupdate
command is defined for theDataAdapter
using thetimestamp
column in theWHERE
clause 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.RowUpdated
Checks 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
. TheDataAdapter
is used to updateDataTable
changes to table TBL0610 in the database. If aDBConcurrencyException
is encountered, the change to the row is rejected.DisplayRow( )
This method is used to display data from a specified row ...
Get ADO.NET Cookbook now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.