vb.net 并发冲突 - UpdateCommand 影响了预期的 1 条记录中的 0 条
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17942689/
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
Concurrency violation - the UpdateCommand affected 0 of the expected 1 records
提问by Jonny Mccoy
I am working on a VB.NET project and I am trying to Insert / Update some rows in the SQL DB.
我正在处理一个 VB.NET 项目,我正在尝试在 SQL DB 中插入/更新一些行。
To do this I use the code below to bring back a dataset based on 3 parameters. If the dataset brings no data back, then the code simply inserts a new row.
为此,我使用下面的代码带回基于 3 个参数的数据集。如果数据集没有带回数据,那么代码只是插入一个新行。
This works fine.
这工作正常。
However, if the data brings a row back (meaning the row already exists in the table) then I want to update one of the values instead. This is where I'm getting the error in the title of this post.
但是,如果数据带回一行(意味着该行已存在于表中),那么我想改为更新其中一个值。这是我在这篇文章的标题中收到错误的地方。
Can anyone help me with where I'm going wrong please?
任何人都可以帮我解决我出错的地方吗?
Thank you in advance.
先感谢您。
Dim x_Update As Boolean
AdaptSql = New Data.AdapterX(SQL_ConnectionString)
DS = New DatsetX
AdaptSql.Fill(DS, Number, PeriodID, TypeID)
If DS.tbl_A.Count > 0 Then
x_Row = DS.tbl_A(0)
x_Row.BeginEdit()
x_Update = True
Else
x_Row = DS.tbl_A.NewRow
End If
x_Row.Number = Number
x_Row.DateID = PeriodID
x_Row.TypeID = TypeID
x_Row.Value = Value
x_Row.UpdatedDate = Date.Now
If x_Update = False Then DS.tbl_A.Addtbl_ARow(x_Row)
x_Row.EndEdit()
AdaptSql.Update(DS)
x_Row = Nothing
DS.Dispose()
AdaptSql = Nothing
回答by Rex
check the x_Row.RowState before call AdaptSql.Update(DS), for a newly created row, the row state should be "Added", while an existing record should have "Modified" since there was some updates to it.
在调用 AdaptSql.Update(DS) 之前检查 x_Row.RowState,对于新创建的行,行状态应为“已添加”,而现有记录应为“已修改”,因为对其进行了一些更新。

