我有一个表,我想删除具有特定卡序列号的所有行。有多行具有相同的卡序列号。所以我写了这段代码,但它似乎不起作用:
try
{
using (SqlConnection con = new SqlConnection(WF_AbsPres.Properties.Settings.Default.DbConnectionString))
{
con.Open();
SqlCommand command2 = new SqlCommand("DELETE FORM DevInOut where Cardserial='" + textBox5.Text + "'", con);
command2.ExecuteNonQuery();
con.Close();
}
}
catch (SqlException ex)
{
}
如何确保所有行都将被删除。我应该使用程序吗?如何使用程序?
最佳答案
将您的 FORM
更改为 FROM
。
请始终使用 parameterized queries 代替。这种字符串连接对开放SQL Injection 攻击。
using (SqlConnection con = new SqlConnection(WF_AbsPres.Properties.Settings.Default.DbConnectionString))
{
con.Open();
SqlCommand command2 = new SqlCommand("DELETE FROM DevInOut where Cardserial=@Cardserial", con);
commdand2.Parameters.AddWithValue("@Cardserial", textBox5.Text);
command2.ExecuteNonQuery();
con.Close();
}
关于c# - 从表中删除多行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19065546/