我有一個包含 8 個表的資料庫,我填充了我的 datagridview,然后為每個表使用相同的代碼塊保存更改,但我只能保存 8 個表中的 4 個,其他 4 個表在我
Concurrency violation: the UpdateCommand affected 0 of the expected 1 records嘗試保存時出現錯誤他們的變化
這就是我填充dataGridView的方式:
{
string script = "SELECT * FROM hatr.rbt;";
mycon = new MySqlConnection(connect);
mycon.Open();
MySqlDataAdapter ms_data = new MySqlDataAdapter(script, connect);
SD.DataTable table = new SD.DataTable();
ms_data.Fill(table);
DataSet ds = new DataSet();
dataGridView1.DataSource = table;
mycon.Close();
ds.AcceptChanges();
}
這就是我保存更改的方式:
{
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();
DataSet ds = new DataSet();
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;
}
我也知道錯誤發生在以下行:
ms_data.Update(dataGridView1.DataSource as DataTable);
每個表的兩個代碼塊都是相同的,但是由于某種原因需要在 dataGrid 中保存更改的第二個塊給了我一個我之前提到的 updateCommand 錯誤,我不知道為什么我會在某些表和為什么我不為別人得到它。那么也許還有其他方法可以保存dataGridView中的更改?因為這是我現在唯一能想到的
uj5u.com熱心網友回復:
我終于找到了Concurrency violation: the UpdateCommand affected 0 of the expected 1 records錯誤的原因。考慮到我已經在MySql Workbench中創建了我的資料庫,并且我試圖通過我的應用程式中的dataGridView使用它,我發現MySql Workbench和dataGridView中的 FLOAT 欄位之間存在沖突。當您在 MySql Workbench 中創建一個浮點數時,它應該用“0.1”之類的點來寫,并且在 dataGridView 中它應該用“0,1”之類的逗號來寫所以一個浮點欄位但在不同的情況下:在mysql Workbench和dataGridView中有兩種不同的撰寫方式,它會發生錯誤,正如我看到的這種情況,我當然可能在某些方面是錯誤的,但我只是重新創建了所有FLOAT 欄位為 DOUBLE,現在它可以作業了。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/450325.html
