i need to update a few tabless that all have the same field in common.
right now i have a separate update statement for each table like so:
try
{
using (SqlConnection conn = new SqlConnection(cCon.getConn()))
{
using (SqlCommand cmd = conn.CreateCommand())
{
conn.Open();
cmd.CommandText = "update table0 set active= 'N' where id=@id";
cmd.Parameters.Add(new SqlParameter("@id", id));
cmd.ExecuteNonQuery();
cmd.Parameters.Clear();
cmd.CommandText = "update table1 set active= 'N' where id= @id ";
cmd.Parameters.Add(new SqlParameter("@id", id));
cmd.ExecuteNonQuery();
cmd.Parameters.Clear();
cmd.CommandText = "update table2 set active= 'N' where id= @id "
cmd.Parameters.Add(new SqlParameter("@id", id));
cmd.ExecuteNonQuery();
cmd.Parameters.Clear();
cmd.CommandText = "update table4 set active= 'N' where id= @id "
cmd.Parameters.Add(new SqlParameter("@id", id));
cmd.ExecuteNonQuery();
}
}
}
can i optimize the code to make it less calls? maybe combine all updates into 1 commandtext and only execute once?
since it's all the same parameters, i'd think it should be possible? or should i just make a stored procedure?