14

When querying the database with the same query but different parameters, is it better to:

  • do it in a single using,
  • or to create two separate queries?

Example of a single using:

using (SqlCommand addProduct = new SqlCommand(@"insert into [Products].[Products] ([Name], [Price]) values (@name, @price)", sqlConnection))
{
    // Insert the first product.
    addProduct.Parameters.AddWithValue("@name", "Product 1");
    addProduct.Parameters.AddWithValue("@price", 41F);
    int countAffectedRows = addProduct.ExecuteNonQuery();
    Debug.Assert(countAffectedRows == 1, "Wrong number of rows affected.");

    addProduct.Parameters.Clear();

    // Insert the second product.
    addProduct.Parameters.AddWithValue("@name", "Product 2");
    addProduct.Parameters.AddWithValue("@price", 49.9);
    countAffectedRows = addProduct.ExecuteNonQuery();
    Debug.Assert(countAffectedRows == 1, "Wrong number of rows affected.");
}

Example of the same code using two separate queries:

// Insert the first product.
using (SqlCommand addProduct = new SqlCommand(@"insert into [Products].[Products] ([Name], [Price]) values (@name, @price)", sqlConnection))
{
    addProduct.Parameters.AddWithValue("@name", "Product 1");
    addProduct.Parameters.AddWithValue("@price", 41F);
    int countAffectedRows = addProduct.ExecuteNonQuery();
    Debug.Assert(countAffectedRows == 1, "Wrong number of rows affected.");
}

// Insert the second product.
using (SqlCommand addProduct = new SqlCommand(@"insert into [Products].[Products] ([Name], [Price]) values (@name, @price)", sqlConnection))
{
    addProduct.Parameters.AddWithValue("@name", "Product 2");
    addProduct.Parameters.AddWithValue("@price", 49.9);
    int countAffectedRows = addProduct.ExecuteNonQuery();
    Debug.Assert(countAffectedRows == 1, "Wrong number of rows affected.");
}

In my opinion, the second one must be preferred, because:

  • it makes it more clear to see where the SQL command is disposed and how much times it is executed,
  • it is easier to modify if, in future, for some reason, the query must be modified in one case, but not in the other,
  • the first one makes it easy to forget the SqlCommand.Parameters.Clear().

On the other hand, the first sample is more explicit about the fact that the query is the same in both cases, and that only parameters change.

Arseni Mourzenko
  • 50,338
  • 35
  • 112
  • 199
  • 2
    You are right, the second solution is more clean. You should not reuse the same SqlCommand unless you are going to do a hyper-mega performance tuning. – Davita Jan 06 '11 at 23:56
  • 2
    Maybe you should place the insert code into a seperate function and call it twice. – k rey Jan 07 '11 at 00:00

2 Answers2

17

There's very little benefit to reusing the command instance, unless you're planning to call Prepare.

If you're going to run the command many times (dozens or more), then you probably want to create the command, prepare it, execute it in a loop, and then dispose it. The performance gains are significant if you're running the command many times. (You would add the parameters once, though, before you prepare -- not delete and re-add them every time like you're doing in your first code sample. You should change the parameters' values each time, not create new parameters.)

If you're only going to be running the command a handful of times, performance isn't an issue, and you should go with whichever style you prefer. Creating the command each time has the benefit that it's easy to extract into a method so you don't repeat yourself.

Joe White
  • 94,807
  • 60
  • 220
  • 330
0

If by "better" you mean "clearer" or "cleaner", use separate SqlCommand objects. This will also assist with refactoring your code down the road.

If by "better" you mean "faster", re-using the SqlCommand will eliminate the chance that a new SqlConnection will be created (vs. being pulled from the connection pool).