并发冲突:UpdateCommand 影响了预期 1 条记录中的 0 条。 DB concurrencyException 未处理

发布于 2024-10-05 05:05:29 字数 842 浏览 0 评论 0原文

我定义了 2 个数据集和 2 个数据适配器(每个数据集一个)。创建后,我为 2 个 dataAdapter 中的每一个定义了一个 SqlCommandBuilder。到这里一切都很好。 我可以使用 dataAdapter1.Update(dataSet1) 从数据库中很好地添加、修改、删除。但不是按这个顺序:删除、添加、修改。

这是第一个数据集、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);

知道为什么会发生这种情况吗?

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 ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

箜明 2024-10-12 05:05:29

你提供的信息是没有用的。但我可以解释这个错误的含义。

用 ADO.Net 编写的每个更新命令的形式如下:

Update col1, col2 where col1=col1value AND col2=col2value

ADO.Net 保留从数据库中选择的列的值。当它执行更新时,条件是提交时没有任何列发生更改。

您看到错误的原因是因为在执行选择和调用 da2.UpdateChanges(ds2) 之间数据库行发生了更改。如果您查看逻辑,也许您已将行的值选择到两个单独的数据集(或两个不同的线程中),并在执行选择后对其执行更新。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文