从DataGridView自动更新数据库中的值

本文关键字:数据库 更新 DataGridView | 更新日期: 2023-09-27 18:14:41

我目前正在做一个项目,使用MySql与c#相结合。DataGridView的数据由数据库中多个表的连接提供。为了显示数据,我使用了以下工作代码:

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的用户修改某个列("comment"列)。因此,我为CellEndEdit事件分配了一个eventandler,当用户修改允许的列时,就调用adapter.Update(data)。现在它没有执行正确的操作。

为了定义我的update命令,我使用了以下代码:
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()方法的数据包含新数据。

从DataGridView自动更新数据库中的值

请尝试此更新查询是否有效。

UPDATE mushrooms 
SET comment = @comment
WHERE 
l_id=(select id from l where lot=@lot)

您的更新语句不正确。应该是:

"UPDATE m FROM mushrooms m JOIN lots l ON m.lot_id = l.id SET m.comment = @comment WHERE l.lot = @lot"

您是否忘记执行warenlagerMySqlDataAdapter.UpdateCommand?你只是在设置命令和参数,而不是执行它。

我看到的是,当信息更新时,你正在调用update,但是你的update命令没有加载。当你更新行时,你只是调用updateWarenlagerView,但是你在哪里调用initialzeFields呢?

还是我遗漏了代码?

尝试将您的更新代码从CellEndEdit事件移动到CellValueChanged事件,看看是否有效。

试试这个例子:

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());
    }
}