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

前端之家收集整理的这篇文章主要介绍了c# – 从DataGridView自动更新数据库中的值前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在使用 MySql与C#结合使用项目.
DataGridView的数据由DB中多个表的连接提供.要显示数据我使用以下,工作,代码
adapter.SelectCommand = new @R_301_198@Command(
            " 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._@R_301_198@Connection);
dataGridView1.DataSource = data;
adapter.Fill(data);

现在,GUI的用户可以修改某个列(“注释”列).所以我分配了一个eventHandler到CellEndEdit事件,当用户修改了允许的列的adapter.Update(data)被调用.现在这不执行正确的操作.

要定义我的update命令,我使用以下代码

adapter.UpdateCommand = new @R_301_198@Command(
                " UPDATE m" +
                " JOIN  l ON m.l_id = l.id" +
                " SET m.comment = @comment" +
                " WHERE l.lot = @lot",this._@R_301_198@Connection);
adapter.UpdateCommand.Parameters.Add("@comment",@R_301_198@DbType.Text,256,"Bemerkungen");
adapter.UpdateCommand.Parameters.Add("@lot","Lot");

你能解释一下我如何修复我的代码自动更新数据库

编辑:
添加进一步的源代码

private @R_301_198@DataAdapter warenlager@R_301_198@DataAdapter,kunden@R_301_198@DataAdapter;
private DataTable warenlagerData,kundenData;
private DataGridView warenlagerGridView;

private void updateWarenlagerView(object sender,EventArgs e) {
            warenlager@R_301_198@DataAdapter.Update(warenlagerData);
}

private void initialzeFields() {
            warenlagerGridView.CellEndEdit += new DataGridViewCellEventHandler(this.updateWarenlagerView);
            warenlager@R_301_198@DataAdapter = new @R_301_198@DataAdapter();
            warenlagerData = new DataTable();
            }

private void initializeWarenlagerView() {
            warenlager@R_301_198@DataAdapter.SelectCommand = new @R_301_198@Command(
                " 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,'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._@R_301_198@Connection);
            warenlagerGridView.DataSource = warenlagerData;
            warenlager@R_301_198@DataAdapter.Fill(warenlagerData);
            warenlager@R_301_198@DataAdapter.UpdateCommand = new @R_301_198@Command(
                " UPDATE mushrooms AS m" +
                " JOIN lots AS l ON m.lot_id = l.id" +
                " SET m.comment = @comment" +
                " WHERE l.lot = @lot",this._@R_301_198@Connection);
            warenlager@R_301_198@DataAdapter.UpdateCommand.Parameters.Add("@comment","Bemerkungen");
            warenlager@R_301_198@DataAdapter.UpdateCommand.Parameters.Add("@lot","Lot");
        }

这是关于这个问题的整个代码.我100%确定adapter.Update(data)方法调用(调试).而传递给适配器的数据.Update()方法包含新的数据.

解决方法

请尝试这个更新查询它的工作原理.
UPDATE mushrooms 
SET comment = @comment
WHERE 
l_id=(select id from l where lot=@lot)

猜你在找的C#相关文章