贴上代码。问题是能执行,但是数据删除不了。数据库连接正常,能查询增加数据。
try
{
con = new SqlConnection("server=.\sqlexpress;integrated security=true;database=master;");
con.Open();
cmd = new SqlCommand("delete from [table_outside] where 展项名称 = '@disname'",con);
cmd.Parameters.AddWithValue("@disname", "58寸触摸一体机");
cmd.ExecuteNonQuery();
con.Close();
MessageBox.Show("成功删除");
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
MessageBox.Show("查找数据失败,请检查数据库连接是否正常"+ex.Message);
}
cmd = new SqlCommand("delete from [table_outside] where 展项名称 = '@disname'",con);
'@disname' 参数错误,不需要单引号
是你的参数化查询用错了,sql中不要再带引号了
where 展项名称 = @disname
不带引号也试过了,也不行
尝试将参数加上类型Nvarchar
数据库的数据是Nvarchar,C#有这种类型么?
System.Data.SqlDbType.NVarChar
cmd = new SqlCommand("delete from [table_outside] where 展项名称 = '@disname'",con);
'@disname' 参数错误,不需要单引号
你查看代码是不是正常运行。
是不是只运行了MessageBox.Show("成功删除"); 这句,可以调试一下