并发冲突:DeleteCommand影响了预期的1条记录中的0条。 [英] Concurrency violation: The DeleteCommand affected 0 of the expected 1 records.

查看:202
本文介绍了并发冲突:DeleteCommand影响了预期的1条记录中的0条。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Visual Basic中使用Access数据库。在我的程序中,我向数据库添加一个新行,如下所示:



  Dim  cb 作为  OleDb.OleDbCommandBuilder(da)
Dim NewRow As DataRow
NewRow = ds.Tables(QuizName).NewRow()
NewRow.Item ( QuestionNumber)=行+ 2
ds.Tables(QuizName).Rows.Add(NewRow)
da.Update(ds,QuizName)





表格已成功更新。但是,如果我在删除新创建的记录后立即发生错误。下面是我用来从表中删除行的代码:



  Dim  cb 作为  OleDb.OleDbCommandBuilder(da)
ds.Tables(QuizName) .Rows(Row).Delete()
da.Update(ds,QuizName)





其中'Row'是新创建的行的位置。但是,da.Update行有一个错误:



 DBConcurrencyException未处理
并发冲突:DeleteCommand受影响0预期的1条记录。





我不知道如何解决这个问题。似乎数据集或数据库不包含我要删除的行。我可以确认访问数据库确实包含该行(我在创建数据库后打开数据库,并且新行存在)。



任何帮助解决此问题的方法都是非常感谢!

解决方案

您需要在执行添加后重新加载DataTable,以便记录选取所分配的数据库的任何ID。


谢谢戴夫。我之前尝试过,但我必须把它放在错误的位置,因为这基本上解决了我的问题!我还必须在再次填充之前清除DataSet。解决方案位于以下代码的最后两行:

 NewRow = ds.Tables(QuizName).NewRow()
NewRow.Item(< span class =code-string> QuestionNumber)=行+ 2
ds.Tables(QuizName).Rows.Add(NewRow)
da.Update(ds,QuizName)
ds.Clear()
da.Fill(ds,QuizName)


I am working in Visual Basic with an Access Database. In my program, I add a new row to the database as follows:

Dim cb As New OleDb.OleDbCommandBuilder(da)
Dim NewRow As DataRow
NewRow = ds.Tables(QuizName).NewRow()
NewRow.Item("QuestionNumber") = Row + 2
ds.Tables(QuizName).Rows.Add(NewRow)
da.Update(ds, QuizName)



The table is updated successfully. However, if I immediately after delete the newly created record, an error occurs. Below is the code I use to delete the row from the table:

Dim cb As New OleDb.OleDbCommandBuilder(da)
ds.Tables(QuizName).Rows(Row).Delete()
da.Update(ds, QuizName)



Where 'Row' is the location of the newly created row. However, the da.Update line has an error:

DBConcurrencyException was unhandled
Concurrency violation: the DeleteCommand affected 0 of the expected 1 records.



I have no real idea how to fix this problem. It seems that the dataset or the database does not contain the row I want to delete. I can confirm that the access database does contain the row (I open the database after making it, and the new row exists).

Any help remedying this issue would be greatly appreciated!

解决方案

You need to reload your DataTable after you do the Add so the record picks up any ID's the database assigned.


Thanks Dave. I tried that before, but I must have put it in the wrong spot because that essentially solves my problem! I also had to clear the DataSet before filling it again. The solution is in the last two lines of the code below:

NewRow = ds.Tables(QuizName).NewRow()
NewRow.Item("QuestionNumber") = Row + 2
ds.Tables(QuizName).Rows.Add(NewRow)
da.Update(ds, QuizName)
ds.Clear()
da.Fill(ds, QuizName)


这篇关于并发冲突:DeleteCommand影响了预期的1条记录中的0条。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆