如何删除 SQLite 数据库中可能包含相同数据的行,具体取决于在列表框中选择的行

How To Delete Rows in a SQLite Database that may hold the same data depending on which row is selected in a listbox

如标题所示,我想删除数据库中的行,具体取决于用户将 select.The Listbox 列表框中的哪一行。这是我当前的代码,但是当 运行 时,出现 SQL 逻辑错误。

        string SelectedItemToDelete = LBoxCurrentBasket.SelectedItem.ToString();

        sqlite_conn = new SQLiteConnection("Data Source=RetailSystem.db; Version = 3; New = True; Compress = True;");
        sqlite_conn.Open();

        try
        {
            sqlite_cmd = sqlite_conn.CreateCommand();
            sqlite_cmd.CommandText = "DELETE * FROM TblBasket WHERE BasketItemName = @ItemToDelete";
            sqlite_cmd.Parameters.AddWithValue("@ItemToDelete", SelectedItemToDelete);
            SQLiteDataReader sqlite_datareader;
            sqlite_datareader = sqlite_cmd.ExecuteReader();
            sqlite_conn.Close();
            MessageBox.Show("Record Deleted!");
            LBoxCurrentBasket.Refresh();
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.ToString());
        }

还有没有办法将 T-Shirt 条记录合并在一起,这样只有一条记录的数量更大?The Database if it helps

我认为你应该在 SqliteCommand 对象上执行 sqlite_cmd.ExecuteNonQuery(); 方法而不是执行 sqlite_cmd.ExecuteReader();

根据 documentation SqliteCommand.ExecuteNonQuery 方法 return 插入、更新或删除的行数。

所以,我认为我会这样做:

 string SelectedItemToDelete = LBoxCurrentBasket.SelectedItem.ToString();

        sqlite_conn = new SQLiteConnection("Data Source=RetailSystem.db; Version = 3; New = True; Compress = True;");
        sqlite_conn.Open();

        try
        {
            sqlite_cmd = sqlite_conn.CreateCommand();
            sqlite_cmd.CommandText = "DELETE FROM TblBasket WHERE BasketItemName = @ItemToDelete";
            sqlite_cmd.Parameters.AddWithValue("@ItemToDelete", SelectedItemToDelete);
            sqlite_cmd.ExecuteNonQuery();
            MessageBox.Show("Record Deleted!");
            LBoxCurrentBasket.Refresh();
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.ToString());
        }
        finally
        {
          sqlite_conn.Close();
        }

此外,不要忘记将连接字符串和查询字符串等参数传递给您的命令对象,以便对数据库执行操作,正如我所见,您在当前代码状态下没有这样做。