简体   繁体   English

从表中删除多行

[英]Deleting multiple rows from a table

I have a table and I want to delete all rows with a specific card serial . 我有一个表,我想删除具有特定卡序列的所有行。 There are multiple rows with the same card serial .So I wrote this code but it seems that's not working: 有多个行具有相同的卡序列。所以我写了这段代码,但似乎不起作用:

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)
{
}

how can assure all the rows will be deleted . 如何确保删除所有行。 Should I use procedure? 我应该使用程序吗? How do I use procedure? 我该如何使用程序?

Change your FORM to FROM . 将您的FORM更改为FROM

And please always use parameterized queries instead. 请始终使用参数化查询 This kind of string concatenations are open for SQL Injection attacks. 这种字符串连接对SQL注入攻击是开放的。

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();
}

Read more from DELETE (Transact-SQL) DELETE (Transact-SQL)了解更多信息

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

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