views:

46

answers:

2

Consider the following code:

Dim sql = "SELECT * FROM MyTable WHERE value1 = @Param1"

If someCondition Then
   sql = sql + " AND value2 = @Param2"
End If

Dim cmd As New SqlCommand(sql, conn)
cmd.Parameters.AddWithValue("@Param1", param1Value)
cmd.Parameters.AddWithValue("@Param2", param2Value)

Assuming that I built a complex sql statement dynamically that may or may not have included the @Param2 parameter - is there any harm in adding it to the command as a parameter?

My real use-case is obviously far more complicated than this, but in general, is this a pattern I should avoid; and if so, why?

A: 

It is always a best practice to avoid passing in parameters that are not needed. If you pass in a parameter that does not have a value then it is either going to assume that you are looking for a NULL, empty string, or it will give you an error that it cannot process the request.

hav2play21
Just to clarify, the parameters all have a valid value, they just may or may not appear in the resulting sql statement that I'm sending off to the server.
DanP
In that case you still should not have them in your query unless you want the chance of having bad data returned or errors if the parameters are not in the table you are querying.A good example of this would be if you are using a Join on a table to query results. Having a extra parameter in your query can greatly change the data that is to be returned or you will get errors saying that the column doesn't exist.
hav2play21
@hav2play21: I don't think you're following...let me update my question accordingly...
DanP
+1  A: 

The only point I would take note is the fact that if you call .AddWithValue, you leave it up to SQL Server to figure out what the data type of the parameter will be.

SQL Server does a remarkably good job of guessing - but sometimes, it gets it "sub-optimally" and it would be helpful for you to provide the details.

So I personally tend to always use this snippet of code:

SqlParameter aParam = new SqlParameter("@Param1", SqlDbType.VarChar, 50);
aParam.Value = param1Value;

This has two main benefits:

  • you get to define the explicit type, which is important e.g. when using VARCHAR vs. NVARCHAR (otherwise you might incur lots of unnecessary type conversions)
  • you get to define the max length of e.g. string parameters

You could easily wrap this in a e.g. static helper class, or even use it as an extension method.

It's a tad more work, but you get more control, and might avoid unnecessary, time-consuming datatype conversions and other unexpected side effects if you leave it up to SQL Server to guess your types.

marc_s
@marc_s: I'm actually doing that, I just wanted to simplify my example code.
DanP
@DanP: ok, great - that is my only point to be aware of when it comes to using parameters with ADO.NET code
marc_s
@marc_s: What about my original question? is using parameters as I described a bad idea?
DanP
@DanP: no, I don't think so - you might be using up a tad of memory and a tad of processor time for constructing those unneeded params - but other than that, I don't see any downside
marc_s
@marc_s: along with increased network activity (for whatever that's worth)
DanP
@DanP: unless you create an absurdly large number of parameters, I don't think that'll make any big (measurable) difference - but yes, you also generate some additional network traffic (larger messages)
marc_s