How do you handle data concurrency in .NET ?

Answer Posted / sureshsamatham

ADO.NET and Visual Studio use optimistic concurrency,
because the data architecture is based on disconnected data.
Therefore, you need to add business logic to resolve issues
with optimistic concurrency.
(1.optimistic 2.pessimistic 3.Last in wins)
If you choose to use optimistic concurrency, there are two
general ways to determine if changes have occurred: the
version approach (true version numbers or date-time stamps)
and the saving-all-values approach.
The Version Number Approach
In the version number approach, the record to be updated
must have a column that contains a date-time stamp or
version number. The date-time stamp or a version number is
saved on the client when the record is read. This value is
then made part of the update.
One way to handle concurrency is to update only if value in
the WHERE clause matches the value on the record. The SQL
representation of this approach is:
Copy Code
UPDATE Table1 SET Column1 = @newvalue1, Column2 = @newvalue2
WHERE DateTimeStamp = @origDateTimeStamp
Alternatively, the comparison can be made using the version
number:
Copy Code
UPDATE Table1 SET Column1 = @newvalue1, Column2 = @newvalue2
WHERE RowVersion = @origRowVersionValue
If the date-time stamps or version numbers match, the record
in the data store has not changed and can be safely updated
with the new values from the dataset. An error is returned
if they don't match. You can write code to implement this
form of concurrency checking in Visual Studio. You will also
have to write code to respond to any update conflicts. To
keep the date-time stamp or version number accurate, you
need to set up a trigger on the table to update it when a
change to a row occurs.
The Saving-All-Values Approach
An alternative to using a date-time stamp or version number
is to get copies of all the fields when the record is read.
The DataSet object in ADO.NET maintains two versions of each
modified record: an original version (that was originally
read from the data source) and a modified version,
representing the user updates. When attempting to write the
record back to the data source, the original values in the
data row are compared against the record in the data source.
If they match, it means that the database record has not
changed since it was read. In that case, the changed values
from the dataset are successfully written to the database.
Each data adapter command has a parameters collection for
each of its four commands (DELETE, INSERT, SELECT, and
UPDATE). Each command has parameters for both the original
values, as well as the current (or modified) values.
Note: use time stamps to avoid concurrency violations

Is This Answer Correct ?    6 Yes 0 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

how can implement dropdownlist in particular of dataset when try to update?

2519


Explain the namespaces in which .net has the data functionality class.

754


How can I retrieve two tables of data at a time by using data reader? Data reader read and forward only, how is it possible to get 2 tables of data at a time?

710


What are the different ado.net namespaces are available in .net?

734


Explain advantages of ado.net?

797


Explain the difference between ado and ado.net?

684


What is ado.net connection?

663


Explain the various objects in dataset.

705


Define Execute Scalar?

740


How will you fill the gridview by using datatable object at runtime?

748


Which namespaces are used for data access?

839


What is the default Timeout for SqlCommand.CommandTimeout property?

728


What is a string variable?

650


What is the difference between Datareader and Dataset?

744


What are the features of ado.net?

691