并发冲突:更新命令影响了预期的 1 条记录中的 0 条.Visual C# 2010.
本文关键字:记录 2010 Visual 更新 冲突 命令 影响 并发 | 更新日期: 2023-09-27 18:26:12
不起。 如果我不是没有想法,我不会发布这个。 我已经尝试了论坛中的所有内容,但无济于事。
所以我有 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"表中创建一个新记录,并在"reading"表中创建一个新记录(但此记录仍然有一些空字段(。 当用户转到"读取"表时,他会看到新记录,然后通过添加一些列值(从而填充 NULL 字段(来更新该记录。 这是代码:
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.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);
}
this.contactDBDataSet.AcceptChanges();
对!鳍
我收到此错误消息,因为我的工作站设置为与服务器不同的时区。一旦我把它弄回了同一个时区......问题消失了
此错误主要是因为更新对数据库的多个更改而没有在请求之间重新加载更新的数据,并且当您尝试保存(更新(堆叠更改时,可能会发生此错误。
欲了解更多信息,请查看Henk的这个答案,这对我非常有帮助。
这里有一个详细的例子。
这个线程很旧,但我想分享我遇到此错误的情况,以防有人也从互联网搜索中遇到此错误。
我在旧版应用程序中看到此错误消息。 事实证明,这是以前的程序员编写一些逻辑的结果,该逻辑检索了DataTable,从中强行删除了一列,然后允许用户在网格中编辑DataTable。 然后将数据表传递给适配器(在我的例子中是 OracleDataAdapter(以应用任何更改。
所以。。。在将列发送到 SqlDataAdapter 之前,手动从数据表中删除列也可能导致此错误消息。