10

我目前正在开发一个将 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 的用户修改某个列(“评论”列)。所以我为事件分配了一个 eventHandler CellEndEdit,当用户修改了允许的列时,adapter.Update(data)就会调用它。现在这不会执行正确的操作。

为了定义我的更新命令,我使用了以下代码:

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()方法的数据包含新数据。

4

5 回答 5

3

您的更新声明不正确。它应该是:

"UPDATE m FROM mushrooms m JOIN lots l ON m.lot_id = l.id SET m.comment = @comment WHERE l.lot = @lot"
于 2013-05-29T13:43:12.500 回答
3

请尝试此更新查询它的工作原理。

UPDATE mushrooms 
SET comment = @comment
WHERE 
l_id=(select id from l where lot=@lot)
于 2013-05-31T05:06:17.473 回答
2

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

我看到的是您在更新信息时调用更新,但未加载您的更新命令。您只是updateWarenlagerView在更新行时调用,但是您在哪里调用initialzeFields

还是我缺少代码?

于 2013-05-30T17:52:13.117 回答
0

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

于 2013-05-31T16:02:30.047 回答
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());

    }
}
于 2018-07-28T17:20:27.857 回答