简体   繁体   中英

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 .

And please always use parameterized queries instead. This kind of string concatenations are open for SQL Injection attacks.

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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