使用listview从数据库中的表中删除一行

时间:2017-10-05 15:34:31

标签: c# sql-server listview

我想知道如何使用listview和button从数据库中的表中删除一行。

我的代码

private void deleteitems()
{
    //DELETE FROM Tbl_Cashier WHERE RecpieId = @RecpieId AND IngredientId = @IngredientId
    string query = "delete from Tbl_Cashier where Cashier_ID = '" + listView1.SelectedIndices+"' ";

    using (SqlConnection connection = new SqlConnection(connectionString1))
    using (SqlCommand command = new SqlCommand(query, connection))
    {
        connection.Open();
        command.Parameters.Remove(listView1.SelectedIndices);

        command.ExecuteNonQuery();
        connection.Close();
    }

    MessageBox.Show("You will see the new data with your next restart of the application", "INFO", MessageBoxButtons.OK, MessageBoxIcon.Information);
}

我收到此错误:

  

未处理的类型' System.InvalidCastException'发生在System.Data.dll

中      

其他信息:SqlParameterCollection只接受非null的SqlParameter类型对象,而不接受SelectedIndexCollection对象。

2 个答案:

答案 0 :(得分:0)

你应该删除这一行:
command.Parameters.Remove(listView1.SelectedIndicies)

然后你应该遍历选择指标并为每个选定的项目触发查询,如下所示:

private void deleteitems()
{    
    using (SqlConnection connection = new SqlConnection(connectionString1))
    {
        connection.Open();
        foreach(var index in listView1.SelectedIndicies)
        {
            // Modify this to get the 'cashier_id' from you listView at the specified row index...
            // You should also consider using a prepared query...
            string query = "delete from Tbl_Cashier where Cashier_ID = '" + index +"' ";
            using (SqlCommand command = new SqlCommand(query, connection))
            {
                // consider checking the return value here if the delete command was successful
                command.ExecuteNonQuery();
            }
        }
    }
    MessageBox.Show("You will see the new data with your next restart of the application", "INFO", MessageBoxButtons.OK, MessageBoxIcon.Information);
}

** 未经过测试 **

答案 1 :(得分:0)

当您提到" a Row"时,我们可以假设您只希望从数据库表中删除一行。

另一个重要假设是listview的第一列包含数据库表的Cashier_ID。

因此,这就是你需要做的事情:

private void DeleteSelectedItemFromListView() {

        var cashierId = listView1.FocusedItem.Text;

        string query = "delete from Tbl_Cashier where Cashier_ID=@id;";

        using (SqlConnection con = new SqlConnection(connectionString1)) {

            try {

                con.Open();

                using (SqlTransaction trans = con.BeginTransaction()) {

                    using (SqlCommand com = new SqlCommand(query, con, trans)) {

                        com.Parameters.AddWithValue("id", cashierId);

                        var should_be_one = com.ExecuteNonQuery();

                        if (should_be_one == 1) {

                            trans.Commit();

                        } else {

                            trans.Rollback();

                            throw new Exception("An attempt to delete multiple rows was detected");
                        }

                    }

                }


            } catch (Exception ex) {
                MessageBox.Show(ex.Message);
            } finally {
                con.Close();
            }

        }

    }

但是,如果您想从列表视图中删除多个项目(多个选定项目),那就是另一个故事。