0

Can I use two command object with one open connection in one procedure at VB.NET?

3
  • 1
    Yes. You can use two or more SqlCommand object with one Open connection. Commented Nov 5, 2009 at 9:53
  • 1
    But you can't use one Command object to open two DataReaders. Commented Nov 6, 2009 at 22:19
  • Actually you CAN use one Command object to open two (or more) DataReaders if you set MultipleActiveResultSets=true in the connection string. However using MARS may have slower performance than just doing it the 'normal way' using one Connection per DataReader, your mileage may vary. Commented Sep 28, 2011 at 3:13

2 Answers 2

2

Yes you can. As long as you don't close the connection between commands, it'll work fine.

This is a C# example, but I'm sure you can work it out:

    using (SqlConnection cn = new SqlConnection("Connection String"))
    {

        SqlCommand cmd1 = new SqlCommand("Command1", cn);
        cmd1.CommandType = CommandType.StoredProcedure;

        SqlCommand cmd2 = new SqlCommand("Command2", cn);
        cmd2.CommandType = CommandType.StoredProcedure;

        cn.Open();

        // Execute cmd1
        // Execure cmd2

    }
Sign up to request clarification or add additional context in comments.

Comments

2

Example; kinda pseudo but you should get the concept.

dim cnn as connection 
dim cmd as command 
dim cmd2 as command 
dim str1 as string
dim str2 as string 

cnn.open

cmd.connection = cnn 
cmd.command = str1 
cmd.execute

cmd2.connection = cnn 
cmd2.command = str2
cmd2.execute

cnn.close

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.