并发冲突:UpdateCommand影响了预期的1条记录中的0条.未处理数据库concurrencyException [英] Concurrency violation: the UpdateCommand affected 0 of the expected 1 records. DB concurrencyException was unhandled
问题描述
这是第一个数据集,dataAdapter和sqlCommandBuilder的代码:
string sql = "SELECT * From localitati";
da1 = new System.Data.SqlClient.SqlDataAdapter(sql, con);
da1.Fill(ds1, "localitati");
cmdBuilder1 = new SqlCommandBuilder(da1);
第二个:
sql = "SELECT * From sucursale";
da2 = new System.Data.SqlClient.SqlDataAdapter(sql, con);
da2.Fill(ds2, "sucursale");
//face automat select, insert ,etc !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
cmdBuilder2 = new SqlCommandBuilder(da2);
任何想法为什么会发生这种情况?
您提供的信息无用.但是我可以解释该错误的含义.
用ADO.Net编写的每个更新命令的形式为:
Update col1, col2 where col1=col1value AND col2=col2value
从数据库中选择列时,ADO.Net会保留该列的值.当它执行更新时,条件是提交时所有列都没有更改.
看到错误的原因是因为在执行选择和调用da2.UpdateChanges(ds2)
之间数据库行已更改.如果您看一下逻辑,也许您已将该行的值选择到两个单独的数据集中(或在两个不同的线程中),并在执行选择后对其进行了更新.
i have defined 2 datasets and 2 dataAdapters( one for each of the datasets ) . after creating, for each of the 2 dataAdapters i define a SqlCommandBuilder. All is well till here. I can add, modify, erase very ok from the database using dataAdapter1.Update(dataSet1) .. BUT not in this order :erase, add,modify.
Here is the code for the first dataset,dataAdapter and sqlCommandBuilder :
string sql = "SELECT * From localitati";
da1 = new System.Data.SqlClient.SqlDataAdapter(sql, con);
da1.Fill(ds1, "localitati");
cmdBuilder1 = new SqlCommandBuilder(da1);
And the second :
sql = "SELECT * From sucursale";
da2 = new System.Data.SqlClient.SqlDataAdapter(sql, con);
da2.Fill(ds2, "sucursale");
//face automat select, insert ,etc !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
cmdBuilder2 = new SqlCommandBuilder(da2);
Any ideas why is this happening ?
Your information supplied is useless. But I can explain the meaning of the error.
Every update command written in ADO.Net is of the form:
Update col1, col2 where col1=col1value AND col2=col2value
ADO.Net keeps the value of the column when it was selected from the database. When it performs the update the condition is that none of the columns have changed when you commit.
The reason you see the error is because the database row has changed in between you performing the select and calling da2.UpdateChanges(ds2)
. If you look at the logic perhaps you have selected the value for the row into two separate datasets (or in two different threads) and performed an update to it after performing the select.
这篇关于并发冲突:UpdateCommand影响了预期的1条记录中的0条.未处理数据库concurrencyException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!