开发者

How Do I Resolve Concurrency Violations with DataTables in .NET?

I'm using DataTables in VB.NET 2.0, and I'm writing code to handle the concurrency violations I expect to occur in a multi-user environment. I understand how to trap concurrency violations, but I don't understand how to resolve them while using the DbCommandBuilder-generated INSERT, UPDATE, and DELETE commands.

It seems to me that concurrency violations occur in five different situations:

When...

  1. Updating a row that has been concurrently updated in the database
  2. Updating a row that has been concurrently deleted from the database
  3. Deleting a row that has been concurrently updated in the database
  4. Deleting a row that has been concurrently deleted from the database
  5. Inserting a row whose primary key has been concurrently inserted into the database

I know how to resolve situation 1; I've found several examples of how to use the DataTable.Merge method to synchronize updated rows. However, Merge doesn't seem to work for the oth开发者_高级运维er 4 situations.

As an example, suppose I'm trying to update a row that has been concurrently deleted from the database (situation 2), and my business rules tell me to restore the row into the database with the updated column values. I can't figure out how to change the DataRow properties so that the Adapter.Update method will insert the row into the database as required; Merge doesn't do it.

Given the ease with which Merge handles situation 1, I'm certain there must be simple resolutions to the other concurrency violations as well, and I've just overlooked them. Can anyone provide me with a nudge in the right direction?

-TC


If the row you are trying to update was deleted by another process, and you need to restore it, then you need to catch the concurrency exception that occurs when you try to update and respond by inserting the row back into the database:

  1. Save the updated values.
  2. Merge the concurrency violations into your table, which will delete the row that was deleted in the database.
  3. Insert a new row into your table with the saved values.
  4. Save the table.
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜