并发冲突:UpdateCommand影响了预期的1条记录中的0条。Visual C#2010

并发冲突:UpdateCommand影响了预期的1条记录中的0条。Visual C#2010,c#,C#,对不起。如果我没有想法的话,我不会发布这个。我在论坛上尝试了一切,但都没有用 所以我有两张桌子。开头两个都是空的。第一个表有点像客户主列表,用户在其中创建一个新的客户配置文件。代码如下: con.Open(); System.Data.SqlClient.SqlCommandBuilder cb; System.Data.SqlClient.SqlCommandBuilder readingcb; cb = new System.D

对不起。如果我没有想法的话,我不会发布这个。我在论坛上尝试了一切,但都没有用

所以我有两张桌子。开头两个都是空的。第一个表有点像客户主列表,用户在其中创建一个新的客户配置文件。代码如下:

        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();
            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");
此部分完成后,我的程序将在“custMaster”表中创建一条新记录,在“reading”表中也将创建一条新记录(但此记录仍有一些空字段)。当用户转到“reading”表时,他会看到新记录,然后通过添加一些列值(从而填充空字段)来更新该记录。代码如下:

        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();
            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");
我不明白为什么会这样,因为我在其他表单中使用了相同的代码更新行,但它们工作得很好。任何帮助都将不胜感激。谢谢


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

我也有同样的问题。但我使用的是Visual studio 2010和C#以及windows窗体。我通过将“this.contactDBDataSet.AcceptChanges();放在“this.tableAdapterManager.UpdateAll(this.contactDBDataSet)”之前解决了我的问题;“。这是样品

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);


}

没错!!FIN

我收到了这个错误消息,因为我的工作站被设置为与我的服务器不同的时区。一旦我将它恢复到同一时区…问题就消失了。

这个错误主要是因为在请求之间和尝试重新加载更新的数据时更新了数据库的多个更改o保存(更新)堆叠更改可能会发生此错误

要了解更多信息,请查看这篇对我非常有用的文章。

而且。

我知道这个线程很旧,但我想和大家分享一下我遇到这个错误的情况,以防有人在互联网搜索中遇到这个错误

我在一个旧版应用程序中看到了此错误消息。它原来是一位程序员编写了一些逻辑的结果,该逻辑检索了一个数据表,强行从中删除了一列,然后允许用户在网格中编辑该数据表。然后将该数据表传递给适配器(在我的例子中是OracleDataAdapter)应用任何更改


因此…在将列发送到SqlDataAdapter之前,手动从DataTable中删除该列也会导致此错误消息。

您需要查看更新命令(即SQL)以确保其正确性。我想这与我尝试的AcceptChanges()中的问题相同方法多次使用,但对我的情况没有帮助。我知道在每次更新时加载数据不是很有趣,但我的问题只有在从_db()调用reload_data_时才得到解决方法在每次更新后再次填充bindingsource。@MojtabaRezaeian,我还必须确保重新填充本地数据集以避免并发异常,但这是因为我的数据库中有一个ModifiedDate列,该列由触发器更新。这会使我的本地数据与数据库不同步,除非我重新填充。这是罕见的这种情况应该很难找到并调试!当我调用
AcceptChanges
时,我没有得到并发错误,但数据库中没有更新行。看起来调用AcceptChanges会将行状态设置为未更改(数据集上的某种提交)。然后调用update将看不到任何更改,因此数据源不会发生任何变化。adapter.update在完成更新后在内部调用acceptchanges。更多信息请参见