在Ado.Net Update winth事务中“并发冲突”

时间:2011-12-02 17:09:31

标签: mysql concurrency transactions ado.net

我在MySQL 5.5,C#,ADO.NET,DataSet上写。 我有一个DataSet和DataAdapter填充它。在DataAdapter中,我重写INSERT,UPDATE,DELETE命令。当我使用存储过程时,作为这些命令,一切都很好。 但是,当我在这个存储过程中使用事务时,我得到一个例外:

  

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

我可以对这些错误做些什么?

示例:http://dl.dropbox.com/u/46828938/DataGridSample.zip

要重现此问题:从Datagrid中删除行并按“更新”按钮。

  

DBConcurrencyException(“并发冲突:DeleteCommand影响预期的1条记录中的0条。”)重现:“productDataAdapter.Update(dataSet,”Products“);”字符串。

2 个答案:

答案 0 :(得分:1)

我学习了很多关于这个问题的文档(msdn,mysql论坛,stackoverflow等),并看到:

检查真正的并发性或不可能做其他查询:

  1. insert:获取最后插入的行并与插入的行进行比较。
  2. update:获取更新的行(例如按键)并与更新的行进行比较。
  3. delete:尝试获取行(例如按键)并获取null。
  4. 当然,上述所有比较都是在一个逻辑浴/连接等中进行的。 不幸的是,其他方式检查“并发违规”或不存在MySql'不存在。(与其他数据库服务器的差异)。

    我希望,这些信息有助于某人。

答案 1 :(得分:0)

SqlCommand com = new SqlCommand("insert into student_info values(@id,@name,@batch)",con);
        com.Parameters.Add(new SqlParameter("@id", dataGridView1.CurrentRow.Cells[0].Value));
        com.Parameters.Add(new SqlParameter("@name", dataGridView1.CurrentRow.Cells[1].Value) );
        com.Parameters.Add(new SqlParameter("@batch", dataGridView1.CurrentRow.Cells[2].Value));
        da.InsertCommand = com;


        com = new SqlCommand("update student_info set name = @name, batch = @batch where s_id = @id", con);
        com.Parameters.Add(new SqlParameter("@id", dataGridView1.CurrentRow.Cells[0].Value));
        com.Parameters.Add(new SqlParameter("@name", dataGridView1.CurrentRow.Cells[1].Value));
        com.Parameters.Add(new SqlParameter("@batch", dataGridView1.CurrentRow.Cells[2].Value));
        da.UpdateCommand = com;

        com = new SqlCommand("delete from student_info where s_id = @id", con);
        com.Parameters.Add(new SqlParameter("@id", dataGridView1.Rows[(dataGridView1.CurrentRow.Index -1)].Cells[0].Value));

您使用上述方法吗?幸运的是,我已经找到了这个例外的原因,但仍在寻找答案。 出现错误是因为我们正在使用dataGridView的CurrentRow属性。它使用insert和update命令工作正常。但是当我们选择cuurentRow for delete命令时,datagridview实际上会选择下一行。如果存在下一条记录则会被删除,否则会抛出这样的错误。我还在寻找答案。当你使用CommandBuilder时,它运行正常。

相关问题