保存对db的更改

时间:2012-05-31 06:37:49

标签: c# datagridview

当我从DataGridView删除一行时,我的数据库没有得到更新。 假设我按Delete键删除DataGridView上的选定行,然后对其他行进行其他一些更改。

最后,我会按Save按钮保存所有这些更改

这是我的代码:

    private bool SaveData(DataGridView dgv)
        {
            bool bDone = true;
            DataTable dtVesselDataChanges;
            SQLiteCommand DeleteCmd = new SQLiteCommand();
            string sdeleteVesselCase = "  public string SQL_DeleteVesselDataCase = "delete from vesseldata where plantid = @plantid and fileid=@fileid and vdid=@vdid";"
            DeleteCmd.CommandText = sdeleteVesselCase;
 dtVesselDataChanges = gdt.GetChanges();

            if (dtVesselDataChanges.Rows.Count > 0 && dtVesselDataChanges != null)
            {
                for (int i = 0; i < dtVesselDataChanges.Rows.Count; i++)
                {
                    if (dtVesselDataChanges.Rows[i].RowState == DataRowState.Deleted)
                    {
                        int rowindexPlant = Convert.ToInt32(gdt.Rows[i]["PlantId", DataRowVersion.Original]);
                        int rowindexFile = Convert.ToInt32(gdt.Rows[i]["FileId", DataRowVersion.Original]);
                        int rowindexVDID = Convert.ToInt32(gdt.Rows[i]["VDID", DataRowVersion.Original]);
                        DeleteCmd.Parameters.AddWithValue("@plantid", rowindexPlant);
                        DeleteCmd.Parameters.AddWithValue("@fileid", rowindexFile);
                        DeleteCmd.Parameters.AddWithValue("@VDID", rowindexVDID);
                        _DatabaseConnection.ExecuteQuery(DeleteCmd); ;

                    }
                }
            }
There is no error except DB is not updated

1 个答案:

答案 0 :(得分:0)

您需要具有正确的删除命令

DeleteCmd.CommandText = "delete from vesseldata where plantid = @plantid and fileid= @fileid and vdid=@vdid";

在命令文本中设置具有相同名称的参数。

DeleteCmd.Parameters.AddWithValue("@plantid", rowindexPlant);
DeleteCmd.Parameters.AddWithValue("@fileid", rowindexFile);
DeleteCmd.Parameters.AddWithValue("@vdid", rowindexVDID);

如果_DatabaseConnection尚未打开,请在ExecuteQuery之前打开ExecuteQuery

如果您的应用程序没有到达{{1}}行,调试并检查您的应用程序是否到达那一点以及命令文本,连接状态和任何错误是什么,您将不会收到任何错误。