How to run multiple SQL commands in a single SQL connection?

前端 未结 9 995
野的像风
野的像风 2020-11-27 15:03

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         


        
9条回答
  •  一整个雨季
    2020-11-27 15:41

    Here you can find Postgre example, this code run multiple sql commands (update 2 columns) within single SQL connection

    public static class SQLTest
        {
            public static void NpgsqlCommand()
            {
                using (NpgsqlConnection connection = new NpgsqlConnection("Server = ; Port = ; User Id = ; " + "Password = ; Database = ;"))
                {
                    NpgsqlCommand command1 = new NpgsqlCommand("update xy set xw = 'a' WHERE aa='bb'", connection);
                    NpgsqlCommand command2 = new NpgsqlCommand("update xy set xw = 'b' where bb = 'cc'", connection);
                    command1.Connection.Open();
                    command1.ExecuteNonQuery();
                    command2.ExecuteNonQuery();
                    command2.Connection.Close();
                }
            }
        }
    

提交回复
热议问题