Dhi
Dhi

Reputation: 157

Run one SqlCommand for two queries perfomance

Till now i'm creating two sql command for running two different queries. I wondering if the performance will change if i will run the same sql command for execute this two queries

Here is my method till now

using (SqlConnection connection = new SqlConnection(connectionString))
{
connection.Open();
using (SqlCommand command1 = new SqlCommand(commandText1, connection))
{
}
using (SqlCommand command2 = new SqlCommand(commandText2, connection))
{
}
// etc
}

Method 2

var command = new SqlCommand("<SQL Command>", myConnection);
    command.ExecuteNonQuery();


command.CommandText = "<New SQL Command>";
   command.ExecuteNonQuery();

Is there any difference in performance or it doesn't matter what i will use.

Upvotes: 0

Views: 133

Answers (1)

MindSwipe
MindSwipe

Reputation: 7855

The performance difference is negligible, but if you for ever and always want to run those exact two queries Method 2 saves opening a new connection to the Database. But you'll want to call command.Parameters.Clear(); before setting the new command.CommandText property

Upvotes: 2

Related Questions