I have a sqlhelper class that includes an overloaded ExecuteNonQuery: one with just one parameter (commandText) and another one with two parameters (commandText, SqlParameter[]).
Assuming I have a stand-alone console application with no user interaction, and I will call a stored procedure that will just update a table with 3 parameters, what are the benefits of using SqlParameter[] if I can just as easily build the string and just send it as commandText?
In other words, why use the following:
SqlParameter[] parameters =
{
new SqlParameter("parm1" SqlDbType.VarChar, 3),
new SqlParameter("parm2", SqlDbType.VarChar, 8),
new SqlParameter("parm3", SqlDbType.VarChar, 2),
new SqlParameter("parm4", SqlDbType.VarChar, 4)
};
parameters[0].Value = p1;
parameters[1].Value = p2;
parameters[2].Value = p3;
parameters[3].Value = p4;
When I can use something like this:
strQueryToRun = string.Format("exec updateTable {0}, {1}, {2}, {3}", p1, p2, p3, p4);
This is a stand-alone console application so there's no possibility of sql injection.
Thanks.
SELECT * from T1 where A=@a1 AND b=@b1