如何修复并发冲突?

问题描述:

我正在使用Visual Studio 2013,我正在VB.Net中创建我的项目。我有一个简单的vb.net表单,其中包含三个绑定到Access数据库数据源的文本字段。 (数据库确实有一个主键。)当我运行项目,添加记录并保存它,对同一记录进行更改然后保存时,会发生并发冲突。完整的错误消息显示:



并发冲突:UpdateCommand影响了预期的1条记录中的0条。在您的应用程序中包含逻辑以处理并发冲突。



我是VB.Net的新手,我迫切需要帮助处理并发冲突的逻辑。我将非常感谢修复此问题所涉及的代码的任何帮助。

I am using Visual Studio 2013 and I am creating my project in VB.Net. I have a simple vb.net form with three text fields that are bound to an Access database data source. (The database does have a primary key.) The concurrency violation occurs when I run the project, add a record and save it, make a change to the same record and then save it. The full error message reads:

Concurrency violation: the UpdateCommand affected 0 of the expected 1 records. Include logic in your application to handle concurrency violations.

I am new to VB.Net and I desperately need help with the logic to handle the concurrency violations. I will greatly appreciate any help with the code involved in fixing this issue.

请阅读我对该问题的评论。



Please, read my comment to the question.

MSDN写道:

当两个用户尝试更改数据库中的相同数据时,会引发并发异常(DBConcurrencyException)同一时间。在本演练中,您将创建一个Windows应用程序,该应用程序说明了捕获DBConcurrencyException,查找导致错误的行,以及一个可用于处理它的策略。

Concurrency exceptions (DBConcurrencyException) are raised when two users attempt to change the same data in a database at the same time. In this walkthrough you create a Windows application that illustrates catching a DBConcurrencyException, locating the row that caused the error, and one strategy for you can use for handling it.





来源:演练:处理并发异常 [ ^ ]



参见:并发冲突:UpdateCommand影响了预期的1条记录中的0条。 [ ^ ]


我是并发问题的原始海报。以下是我在应用程序中解决并发冲突问题的方法。它可能不适合其他应用程序。但它可以帮助其他新用户。



我的应用程序是针对单个用户的。因此,永远不会有两个用户同时尝试更改数据库表中的记录的实例。所以我不需要捕获并发冲突。当我输入数据,保存记录,然后修改记录并再次保存时,我的应用程序中抛出了并发冲突错误。当没有两个用户改变相同记录的情况时,让程序抛出错误令人沮丧。我正在对我刚刚输入的内容进行修改,这就是我想要的。



I am the original poster of the concurrency question. Here is how I solved the concurrency violation issue in my application. It may not be appropriate in other applications. But it may help other new users.

My application is for a single user. Therefore, there will never be the instance where two users will by trying to change a record in the database table at the same time. So I do not need to catch concurrency violations. The concurrency violation error was being thrown in my application when I entered data, saved the record, and then modified the record and saved it again. It was frustrating to have the program throw an error when there wasn't a situation where two users where changing the same record. I was making a modification to something that I just entered, and that is what I wanted.

'Save Changes to Records
   Private Sub TblAssetDataBindingNavigatorSaveItem_Click(sender As Object, e As EventArgs) Handles TblAssetDataBindingNavigatorSaveItem.Click
       Me.Validate()
       Me.TblAssetDataBindingSource.EndEdit()
       Me.TableAdapterManager.UpdateAll(Me.AssetDatabaseDataSet)
       Me.TblAssetDataTableAdapter.Fill(Me.AssetDatabaseDataSet.tblAssetData)

   End Sub


由于我的应用程序是单个用户应用程序,因此当用户保存更改时,代码没有理由检查并发问题制作成数据库中的记录。因此,我只是在表单的保存事件中使用 On Error Resume Next 来避免烦人的并发错误消息。



当用户在表单中输入数据,然后保存更改,然后修改相同的记录,然后保存它时,我的应用程序中出现并发错误消息再次。显然,如果同一个用户对他们刚刚保存的记录进行了更改,他们希望保存更新的记录,并且没有并发问题。
Since my application is a single user application, there is no reason for the code to check for concurrency issues when the user saves a change they made to a record in the database. Therefore, I just use On Error Resume Next in the form's Save event to avoid the annoying concurrency error message.

The concurrency error message was occurring in my application when the user entered data into the form, then saved the change, and subsequently modified that same record, and then saved it again. Obviously, if the same user makes a change to a record that they just saved, they want to save the updated record and there is no concurrency issue.