Skip to main content
 首页 » 编程设计

c#之从表中删除多行

2024年02月13日16cloudgamer

我有一个表,我想删除具有特定卡序列号的所有行。有多行具有相同的卡序列号。所以我写了这段代码,但它似乎不起作用:

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

阅读更多 DELETE (Transact-SQL)