我目前正在使用MySql与C#结合的项目。 DataGridView的数据由DB中多个表的连接提供。为了显示数据,我使用以下工作代码:
adapter.SelectCommand = new MySqlCommand(
" SELECT" +
" l.lot AS Lot, "+
" m.comment AS Bemerkungen," +
... (multiple columns from different tables) ...
" FROM m " +
" JOIN m2p ON m.m2p_id = m2p.id" +
... (more joins) ...
, this._mySqlConnection);
dataGridView1.DataSource = data;
adapter.Fill(data);
现在允许GUI的用户修改某个列(“注释”列)。所以我为CellEndEdit
事件分配了一个eventHandler,当用户修改了允许的列时,调用了adapter.Update(data)
。现在这不会执行正确的操作。
要定义我的updatecommand,我使用了以下代码:
adapter.UpdateCommand = new MySqlCommand(
" UPDATE m" +
" JOIN l ON m.l_id = l.id" +
" SET m.comment = @comment" +
" WHERE l.lot = @lot"
, this._mySqlConnection);
adapter.UpdateCommand.Parameters.Add("@comment", MySqlDbType.Text, 256, "Bemerkungen");
adapter.UpdateCommand.Parameters.Add("@lot", MySqlDbType.Text, 256, "Lot");
您能解释一下我如何修复我的代码以自动更新数据库吗?
编辑: 添加了更多源代码:
private MySqlDataAdapter warenlagerMySqlDataAdapter, kundenMySqlDataAdapter;
private DataTable warenlagerData, kundenData;
private DataGridView warenlagerGridView;
private void updateWarenlagerView(object sender, EventArgs e) {
warenlagerMySqlDataAdapter.Update(warenlagerData);
}
private void initialzeFields() {
warenlagerGridView.CellEndEdit += new DataGridViewCellEventHandler(this.updateWarenlagerView);
warenlagerMySqlDataAdapter = new MySqlDataAdapter();
warenlagerData = new DataTable();
}
private void initializeWarenlagerView() {
warenlagerMySqlDataAdapter.SelectCommand = new MySqlCommand(
" SELECT" +
" c.name AS Ursprung, " +
" m2p.art_nr AS ArtNr," +
" m.delivery_date AS Eingangsdatum," +
" CONCAT(FORMAT(m.delivery_amount / 100, 2), 'kg') AS Eingangsmenge, " +
" l.lot AS Lot," +
" m.quality AS Qualität," +
" m.comment AS Bemerkungen," +
" CONCAT(m.units, 'kg') AS Units," +
" CONCAT(FORMAT(s.amount / 100, 2), 'kg') AS Lagermenge, " +
" FORMAT(m.base_price / 100, 2) AS Einkaufspreis," +
" FORMAT(s.amount/10000 * m.base_price, 2) AS Wert" +
" FROM mushrooms AS m " +
" JOIN mushroom2path AS m2p ON m.mushroom2path_id = m2p.id" +
" JOIN countries AS c ON m.origin_id = c.id" +
" JOIN lots AS l ON m.lot_id = l.id" +
" JOIN stock AS s ON s.mushrooms_id = m.id"
, this._mySqlConnection);
warenlagerGridView.DataSource = warenlagerData;
warenlagerMySqlDataAdapter.Fill(warenlagerData);
warenlagerMySqlDataAdapter.UpdateCommand = new MySqlCommand(
" UPDATE mushrooms AS m" +
" JOIN lots AS l ON m.lot_id = l.id" +
" SET m.comment = @comment" +
" WHERE l.lot = @lot"
, this._mySqlConnection);
warenlagerMySqlDataAdapter.UpdateCommand.Parameters.Add("@comment", MySqlDbType.Text, 256, "Bemerkungen");
warenlagerMySqlDataAdapter.UpdateCommand.Parameters.Add("@lot", MySqlDbType.Text, 256, "Lot");
}
这是关于这个问题的整个代码。我100%确定调用adapter.Update(data)
方法(调试)。传递给adapter.Update()
方法的数据包含新数据。
答案 0 :(得分:3)
您的更新声明不正确。它应该是:
"UPDATE m FROM mushrooms m JOIN lots l ON m.lot_id = l.id SET m.comment = @comment WHERE l.lot = @lot"
答案 1 :(得分:3)
请尝试使用此更新查询。
UPDATE mushrooms
SET comment = @comment
WHERE
l_id=(select id from l where lot=@lot)
答案 2 :(得分:2)
您是否忘记执行warenlagerMySqlDataAdapter.UpdateCommand? 您只需设置命令和参数,但不执行它。
我看到的是您在更新信息时调用更新,但未加载更新命令。
您只需在更新行时调用updateWarenlagerView
,但在哪里调用initialzeFields
?
或者我错过了代码?
答案 3 :(得分:0)
尝试将更新代码从CellEndEdit
事件移至CellValueChanged
事件,看看是否有效。
答案 4 :(得分:0)
尝试以下示例:
public void UpdateAllFromDgv(DataGridView dataGridView1)
{
string query = "Update List set ColumnName1=@Value1" +
",ColumnName2=@Value2" +
",ColumnName3=@Value3" +
",ColumnName4=@Value4" +
",ColumnName5=@Value5" +
",ColumnName6=@Value6 where ColumnName0=@Value0";
try
{
foreach (DataGridViewRow row in dataGridView1.Rows)
{
using (MySqlConnection con = new MySqlConnection(ConnectionString))
{
using (MySqlCommand cmd = new MySqlCommand(query, con))
{
cmd.Parameters.AddWithValue("@Value0", row.Cells[0].Value);
cmd.Parameters.AddWithValue("@Value1", row.Cells[1].Value);
cmd.Parameters.AddWithValue("@Value2", row.Cells[2].Value);
cmd.Parameters.AddWithValue("@Value3", row.Cells[3].Value);
cmd.Parameters.AddWithValue("@Value4", row.Cells[4].Value);
cmd.Parameters.AddWithValue("@Value5", row.Cells[5].Value);
cmd.Parameters.AddWithValue("@Value6", row.Cells[6].Value);
con.Open();
cmd.ExecuteNonQuery();
dataGridView1.ResetBindings();
con.Close();
}
}
}
}
catch (MySqlException MsE)
{
MessageBox.Show(MsE.Message.ToString());
}
}