sql server - Unused sql parameters - are they of any harm? -
consider following code:
dim sql = "select * mytable value1 = @param1" if somecondition sql = sql + " , value2 = @param2" end if dim cmd new sqlcommand(sql, conn) cmd.parameters.addwithvalue("@param1", param1value) cmd.parameters.addwithvalue("@param2", param2value)
assuming built complex sql statement dynamically may or may not have included @param2
parameter - there harm in adding command parameter?
my real use-case far more complicated this, in general, pattern should avoid; , if so, why?
the point take note fact if call .addwithvalue
, leave sql server figure out data type of parameter be.
sql server remarkably job of guessing - sometimes, gets "sub-optimally" , helpful provide details.
so tend use snippet of code:
sqlparameter aparam = new sqlparameter("@param1", sqldbtype.varchar, 50); aparam.value = param1value;
this has 2 main benefits:
- you define explicit type, important e.g. when using varchar vs. nvarchar (otherwise might incur lots of unnecessary type conversions)
- you define max length of e.g. string parameters
you wrap in e.g. static helper class, or use extension method.
it's tad more work, more control, , might avoid unnecessary, time-consuming datatype conversions , other unexpected side effects if leave sql server guess types.
Comments
Post a Comment