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

前端 未结 9 975
野的像风
野的像风 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

    Just change the SqlCommand.CommandText instead of creating a new SqlCommand every time. There is no need to close and reopen the connection.

    // Create the first command and execute
    var command = new SqlCommand("<SQL Command>", myConnection);
    var reader = command.ExecuteReader();
    
    // Change the SQL Command and execute
    command.CommandText = "<New SQL Command>";
    command.ExecuteNonQuery();
    
    0 讨论(0)
  • 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();
                }
            }
        }
    
    0 讨论(0)
  • 2020-11-27 15:43

    The following should work. Keep single connection open all time, and just create new commands and execute them.

    using (SqlConnection connection = new SqlConnection(connectionString))
    {
        connection.Open();
        using (SqlCommand command1 = new SqlCommand(commandText1, connection))
        {
        }
        using (SqlCommand command2 = new SqlCommand(commandText2, connection))
        {
        }
        // etc
    }
    
    0 讨论(0)
  • 2020-11-27 15:43

    This is likely to be attacked via SQL injection by the way. It'd be worth while reading up on that and adjusting your queries accordingly.

    Maybe look at even creating a stored proc for this and using something like sp_executesql which can provide some protection against this when dynamic sql is a requirement (ie. unknown table names etc). For more info, check out this link.

    0 讨论(0)
  • 2020-11-27 15:46
    using (var connection = new SqlConnection("Enter Your Connection String"))
        {
            connection.Open();
        
            using (var command = connection.CreateCommand())
            {
                command.CommandText = "Enter the First Command Here";
                command.ExecuteNonQuery();
        
                command.CommandText = "Enter Second Comand Here";
                command.ExecuteNonQuery();
    
        //Similarly You can Add Multiple
            }
        }
    
    0 讨论(0)
  • 2020-11-27 15:54

    Multiple Non-query example if anyone is interested.

    using (OdbcConnection DbConnection = new OdbcConnection("ConnectionString"))
    {
      DbConnection.Open();
      using (OdbcCommand DbCommand = DbConnection.CreateCommand())
      {
        DbCommand.CommandText = "INSERT...";
        DbCommand.Parameters.Add("@Name", OdbcType.Text, 20).Value = "name";
        DbCommand.ExecuteNonQuery();
    
        DbCommand.Parameters.Clear();
        DbCommand.Parameters.Add("@Name", OdbcType.Text, 20).Value = "name2";
        DbCommand.ExecuteNonQuery();
      }
    }
    
    0 讨论(0)
提交回复
热议问题