简体   繁体   English

C#数据库列表框项删除

[英]c# database listbox item remove

I making a database and I can add names, but I can't remove please help me. 我正在建立数据库,可以添加名称,但是不能删除,请帮帮我。

This is my code: 这是我的代码:

private void button2_Click(object sender, EventArgs e)
{
    using (connection = new SqlConnection(connectionString))
    using (SqlCommand cmds = new SqlCommand("DELETE FROM Recipe WHERE firstName = @user", connection))
    {
        connection.Open();
        cmds.Parameters.AddWithValue("@user", cmds);
        cmds.ExecuteNonQuery();

    }
    populateRecipe();
}

This is the error 这是错误

An unhandled exception of type System.ArgumentException occurred in System.Data.dll Additional information: No mapping exists from object type System.Data.SqlClient.SqlCommand to a known managed provider native type. System.Data.dll Additional发生了类型为System.ArgumentException的未处理异常。 System.Data.dll Additional信息:不存在从对象类型System.Data.SqlClient.SqlCommand到已知的托管提供程序本机类型的映射。

Instead of passing the cmds itself to the AddWithValue method : 取而代之的传递CMDS本身的AddWithValue方法:

cmds.Parameters.AddWithValue("@user", cmds);

I suppose you should pass your actual uesrname : 我想您应该通过实际的uesrname:

cmds.Parameters.AddWithValue("@user", username);

You are passing the cmds variable as the value for the @user parameter which doesn't make sense because it's a SqlCommand object: 您将cmds变量作为@user参数的值传递,这没有意义,因为它是SqlCommand对象:

cmds.Parameters.AddWithValue("@user", cmds);

Don't you intend to pass the user's name that you want to delete instead? 您不是要传递要删除的用户名吗?

cmds.Parameters.AddWithValue("@user", "Username");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM