I am creating a project in which I need to run 2-3 SQL commands in a single SQL connection. Here is the code I have written:
SqlConnection con = new SqlConn
No one has mentioned this, but you can also separate your commands using a ; semicolon in the same CommandText:
using (SqlConnection conn = new SqlConnection(connString))
{
using (SqlCommand comm = new SqlCommand())
{
comm.Connection = conn;
comm.CommandText = @"update table ... where myparam=@myparam1 ; " +
"update table ... where myparam=@myparam2 ";
comm.Parameters.AddWithValue("@myparam1", myparam1);
comm.Parameters.AddWithValue("@myparam2", myparam2);
conn.Open();
comm.ExecuteNonQuery();
}
}