gpt4 book ai didi

c# - 并发冲突 : the UpdateCommand affected 0 of the expected 1 records. Visual C# 2010

转载 作者:行者123 更新时间:2023-11-30 15:05:03 28 4
gpt4 key购买 nike

对不起。如果我没有想法,我不会发布这个。我已经尝试了论坛中的所有方法,但无济于事。

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

        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”表中创建一条新记录,并在“reading”表中创建一条新记录(但该记录仍有一些空字段)。当用户转到“阅读”表时,他会看到新记录,然后通过添加一些列值(从而填充空字段)来更新该记录。这是代码:

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

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

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

最佳答案

我也遇到了同样的问题。但我正在使用带有 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);


}

关于c# - 并发冲突 : the UpdateCommand affected 0 of the expected 1 records. Visual C# 2010,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9521355/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com