我通过单击按钮将更改保存在数据库表中,这是代码本身:
else if (comboBox1.SelectedItem == "Элек")
{
label3.Text = "Электричество";
string script = "SELECT id, name, Mx_pw_VT, Pr_pw_VT, elec_kVT_hr, Print_time_fact, YZV_power_VT FROM elec;";
mycon = new MySqlConnection(connect);
mycon.Open();
MySqlDataAdapter ms_data = new MySqlDataAdapter(script, connect);
var cb = new MySqlCommandBuilder(ms_data);
cb.GetInsertCommand();
ms_data.Update(dataGridView1.DataSource as DataTable);
ms_data.InsertCommand = cb.GetInsertCommand();
ms_data.InsertCommand.CommandText += "; select * from elec where id = last_insert_id();";
ms_data.InsertCommand.UpdatedRowSource = UpdateRowSource.FirstReturnedRecord;
}
但由于某种原因,它给了我一个错误«Нарушение параллелизма: UpdateCommand затронула 0 из ожидаемых 1 записей.»
。我对另一个表有相同的代码块并且它可以工作,这个表中发生了变化,块本身:
else if (comboBox1.SelectedItem == "Рбт")
{
label3.Text = "Рбт";
string script = "SELECT id, name, Model_preparation_R_Hr, Time_for_preparation_hr, Time_for_post_processing_hr, YZV_work FROM rbt;";
mycon = new MySqlConnection(connect);
mycon.Open();
MySqlDataAdapter ms_data = new MySqlDataAdapter(script, connect);
var cb = new MySqlCommandBuilder(ms_data);
cb.GetInsertCommand();
ms_data.Update(dataGridView1.DataSource as DataTable);
ms_data.InsertCommand = cb.GetInsertCommand();
ms_data.InsertCommand.CommandText += "; select * from rbt where id = last_insert_id();";
ms_data.InsertCommand.UpdatedRowSource = UpdateRowSource.FirstReturnedRecord;
}
怎么了?请告诉我
从数据库获取和转换响应的方法(仅与 SELECT 一起使用):
获取插入的 sql 查询行的方法。方便很多。无需为表格编写 1000 和 1 插入行。你只需要创建一个类:
插入方法:
从 ExecuteNonQuery 方法返回的枚举:
获取数据库行的 GetDBConnection 方法:
一个用于方法正常操作的类的例子:
为 Select 调用 SqlQuery 方法的示例:
为 Insert 调用 ExecuteNonQuery 方法的示例: