【问题标题】:Concurrency violation: the UpdateCommand affected 0 of the expected 1 records. Visual C# 2010并发冲突:UpdateCommand 影响了预期的 1 条记录中的 0 条。视觉 C# 2010
【发布时间】:2012-03-01 17:53:37
【问题描述】:

对不起。如果我没有想法,我不会发布这个。论坛里的东西都试过了,没用。

所以我有 2 张桌子。一开始都是空的。第一个表有点像客户主列表,用户在其中创建新的客户资料。这是代码:

        con.Open();
        System.Data.SqlClient.SqlCommandBuilder cb;
        System.Data.SqlClient.SqlCommandBuilder readingcb;
        cb = new System.Data.SqlClient.SqlCommandBuilder(da);
        readingcb = new System.Data.SqlClient.SqlCommandBuilder(readingda);



        DataRow dRow = custMaster.Tables["custMaster"].NewRow();
        DataRow readingdRow = reading.Tables["reading"].NewRow();
        dRow[1] = a_newCust.Text;
        readingdRow[9] = a_newCust.Text;
        dRow[2] = b_newCust.Text;
        readingdRow[10] = b_newCust.Text;
        dRow[3] = c_newCust.Text;
        readingdRow[11] = c_newCust.Text;
        dRow[4] = d_newCust.Text;
        readingdRow[0] = d_newCust.Text;
        dRow[5] = e_newCust.Text.ToString();
        readingdRow[2] = e_newCust.Text.ToString();
        dRow[6] = f_newCust.Text;
        readingdRow[1] = f_newCust.Text;
        dRow[7] = g_newCust.Text.ToString();
        if (radioButton1.Checked == true)
        {
            dRow[8] = radioButton1.Text;
            readingdRow[3] = radioButton1.Text;
        }
        else if (radioButton2.Checked == true)
        {
            dRow[8] = radioButton2.Text;
            readingdRow[3] = radioButton2.Text;
        }
        dRow[9] = "Active";
        readingdRow[12] = "Active";
        readingdRow[4] = 0;
        readingdRow[5] = 0;


        custMaster.Tables["custMaster"].Rows.Add(dRow);
        reading.Tables["reading"].Rows.Add(readingdRow);


        MaxRows = MaxRows + 1;
        readingMaxRows = readingMaxRows + 1;
        inc = MaxRows - 1;
        readinginc = readingMaxRows - 1;


        this.da.Update(custMaster, "custMaster");
        this.readingda.Update(reading, "reading");


        da.Fill(masterDataSet3.custMaster);
        readingda.Fill(streetDataSet.reading);
        masterfileGrid.Invalidate();
        readingGrid.Invalidate();
        masterfileGrid.Refresh();
        readingGrid.Refresh();

        MessageBox.Show("Customer succesfully added!");

        con.Close();

当这部分完成后,我的程序将在“custMaster”表中创建一条新记录,并在“阅读”表中创建一条新记录(但该记录仍有一些空字段)。当用户转到“阅读”表时,他会看到新记录,然后通过添加一些列值(从而填充空字段)来更新该记录。这是代码:

            con.Open();
            System.Data.SqlClient.SqlCommandBuilder readingup;
            System.Data.SqlClient.SqlCommandBuilder custpayadd;
            readingup = new System.Data.SqlClient.SqlCommandBuilder(readingda);
            custpayadd = new System.Data.SqlClient.SqlCommandBuilder(custpayda);
            int test = int.Parse(f_reading.Text);
            int prev = int.Parse(readingNinBox.Text);              


            DataRow readingdRow = reading.Tables["reading"].Rows[test-1];                  


            readingdRow[4] = prev;
            readingdRow[5] = int.Parse(d_reading.Text);
            readingdRow[13] = e_reading.Value.ToShortDateString();
            readingdRow[6] = int.Parse(d_reading.Text) - prev;


            if (g_reading.Text == "Residential")
            {
                DataRow resSearchdRow = water.Tables["water"].Rows[int.Parse(d_reading.Text) - prev];
                readingdRow[7] = resSearchdRow[2];
            }


            else
            {
                DataRow commSearchdRow = commWater.Tables["commWater"].Rows[int.Parse(d_reading.Text) - prev];
                readingdRow[7] = commSearchdRow[2];
            }               

            this.readingda.Update(reading, "reading"); //>>>>>>>>>>this part is where i get the concurrency error.

            readingda.Fill(streetDataSet.reading);                
            readingGrid.Invalidate();
            readingGrid.Refresh();
//elsewhere:

masterDataSet reading;

System.Data.SqlClient.SqlDataAdapter readingda;

int readingMaxRows = 0;

int readinginc = 0;

this.readingTableAdapter.Fill(this.streetDataSet.reading);

reading = new masterDataSet();

string readingsql = "SELECT * From reading";

readingda = new System.Data.SqlClient.SqlDataAdapter(readingsql, con);

readingda.Fill(reading, "reading");

readingMaxRows = reading.Tables["reading"].Rows.Count;

readingda.Update(reading, "reading");

我不明白为什么会这样,因为我在其他表单中使用了相同的更新代码行,但它们运行良好。任何帮助将不胜感激。谢谢。

一些附加信息:我只在第一次将记录添加到“阅读”表时收到错误。当我关闭并重新打开我的程序时,我可以很好地更新记录。

【问题讨论】:

  • 您需要检查您的更新命令(即 SQL)以确保它是正确的。
  • 我猜这与question中的问题相同

标签: c#


【解决方案1】:

我也有同样的问题。但我正在使用带有 C# 和 Windows 窗体的 Visual Studio 2010。我通过在“this.tableAdapterManager.UpdateAll(this.contactDBDataSet);”之前放置“this.contactDBDataSet.AcceptChanges();”解决了我的问题。这是示例。

private void peopleBindingNavigatorSaveItem_Click_2(object sender, EventArgs e)
{
    this.Validate();
    this.peopleBindingSource.EndEdit();
    this.contactDBDataSet.AcceptChanges();// added by Humayoo
    this.tableAdapterManager.UpdateAll(this.contactDBDataSet);


}

【讨论】:

  • 我多次尝试 AcceptChanges() 方法,但对我的情况没有帮助。我知道在每次更新时加载数据并不是很有趣,但只有当我在每次更新后调用 reload_data_from_db() 方法再次填充我的绑定源时,我的问题才得到解决。
  • @MojtabaRezaeian,我还必须确保重新填充本地数据集以避免并发异常,但这是因为我在数据库中有一个由触发器更新的 ModifiedDate 列.这会使我的本地数据与我的数据库不同步,除非我重新填写。
【解决方案2】:
this.contactDBDataSet.AcceptChanges();

没错!!鳍

【讨论】:

  • 当我调用 AcceptChanges 时,我没有收到并发错误,但数据库中没有更新任何行。
  • 看起来调用 AcceptChanges 会将行状态设置为未更改(数据集上的某种提交)。然后调用 update 将看不到任何更改,因此数据源不会发生任何事情。完成更新后,adapter.update 在内部调用acceptchanges。更多这里docs.microsoft.com/en-us/dotnet/framework/data/adonet/…
【解决方案3】:

我收到此错误消息是因为我的工作站设置为与我的服务器不同的时区。一旦我把它恢复到同一个时区......问题就消失了

【讨论】:

  • 这种罕见的情况应该很难发现和调试!
【解决方案4】:

此错误主要是因为更新多个数据库更改而不重新加载更新的数据在请求之间以及当您尝试保存(更新)堆叠更改时可能会发生此错误。

有关更多信息,请查看此answer by Henk,这对我很有帮助。


还有here there is a good example in details

【讨论】:

    【解决方案5】:

    我知道这个帖子很旧,但我想分享一下我收到此错误的情况,以防有人也通过互联网搜索遇到此错误。

    我在旧版应用中看到此错误消息。结果证明这是由于以前的程序员编写了一些逻辑来检索 DataTable,从中强制删除一列,然后允许用户在网格中编辑 DataTable。然后将 DataTable 传递给适配器(在我的例子中是 OracleDataAdapter)以应用任何更改。

    所以...在将列发送到 SqlDataAdapter 之前从 DataTable 中手动删除该列也可能导致此错误消息。

    【讨论】:

      猜你喜欢
      • 2017-06-08
      • 2010-12-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-17
      相关资源
      最近更新 更多