database 并发冲突:UpdateCommand 影响了预期的 1 条记录中的 0 条

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1248537/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 07:26:26  来源:igfitidea点击:

Concurrency violation: the UpdateCommand affected 0 of the expected 1 records

databasevb.netconcurrency

提问by Kenny Bones

I've got a simple mdb database set up in Visual Basic Express Edition (winforms) and I'm trying to update data from a datagridview to a database. I've databinded the textboxes to the columns I want in the datagridview and this works great.

我在 Visual Basic Express Edition (winforms) 中设置了一个简单的 mdb 数据库,我正在尝试将数据从 datagridview 更新到数据库。我已经将文本框数据绑定到数据网格视图中我想要的列,这很好用。

After some struggling, I've finally managed to have the values updated in every row of the database. But, trying to modify a newly created record throws this error: "Concurrency violation: the UpdateCommand affected 0 of the expected 1 records"

经过一番挣扎,我终于设法在数据库的每一行中更新了值。但是,尝试修改新创建的记录会引发此错误:“并发冲突:UpdateCommand 影响了预期的 1 条记录中的 0 条”

I mean, it seems as though the error only happens with newly created rows. Because closing the application and reopening it immediatly, I'm allowed to update the values of the cells. I also noticed that the ID of the row is set to "-1" when I've just created a new row. The other rows don't have a negative number for ID. Could this be the cause? Seems as though the row isn't really updated to the database.

我的意思是,似乎错误只发生在新创建的行上。因为关闭应用程序并立即重新打开它,所以我可以更新单元格的值。我还注意到,当我刚刚创建一个新行时,该行的 ID 设置为“-1”。其他行的 ID 没有负数。这可能是原因吗?似乎该行并未真正更新到数据库。

So this is the code for the AddRow button

所以这是 AddRow 按钮的代码

Dim newRow As MusicDBDataSet.SongsRow
        newRow = MusicDBDataSet.Songs.NewSongsRow()
        newRow.Name = txtBoxNewName.Text
        newRow.Genre = txtBoxNewGenre.Text
        newRow.Rhytm = txtBoxNewRhytm.Text
        newRow.Length = txtBoxNewLength.Text
        MusicDBDataSet.Songs.Rows.Add(newRow)

        Try
            Me.Validate()
            Me.SongsTableAdapter.Update(Me.MusicDBDataSet.Songs)
            Me.SongsBindingSource.EndEdit()
            Me.MusicDBDataSet.Songs.AcceptChanges()

        Catch ex As Exception
            MsgBox(ex.Message)
        End Try

This fills the data I've put into the text boxes into a new row of the DataGridView. Everything looks fine (except for the -1 value of the ID column)

这会将我放入文本框中的数据填充到 DataGridView 的新行中。一切看起来都很好(除了 ID 列的 -1 值)

The textboxes that are databinded to the database, these are the ones I'm using to try to update the values of the cells. And the code looks like this:

数据绑定到数据库的文本框,这些是我用来尝试更新单元格值的文本框。代码如下所示:

Try
    Me.Validate()
    Me.SongsBindingSource.EndEdit()
    Me.SongsTableAdapter.Update(Me.MusicDBDataSet.Songs)
    Me.MusicDBDataSet.Songs.AcceptChanges()

Catch ex As Exception
    MsgBox(ex.Message)
End Try

Now, I'm not sure at all if this is the correct way of doing this. But thinking about it, the textboxes used to update the values of the newly created row are databinded to the database or tableadapter itself right? Could it be that the UpdateCommand fails because the row hasn't really been created properly yet?

现在,我完全不确定这是否是正确的做法。但是想想看,用于更新新创建行的值的文本框是绑定到数据库或表适配器本身的,对吗?可能是因为该行还没有真正正确创建,所以 UpdateCommand 失败了?

回答by SP007

I resolved it as follows:

我是这样解决的:

After modifying data table do following step.

修改数据表后执行以下步骤。

dataTable = dataTable.GetChanges()

回答by ItchyChy

The easiest way is to open the dataset in designer. Delete the update, insert and delete commands, choose table adapter - configure - advanced options - generate insert, update and delete statements. Leave the second option unchecked.

最简单的方法是在设计器中打开数据集。删除更新、插入和删除命令,选择表适配器——配置——高级选项——生成插入、更新和删除语句。不选中第二个选项。