No, .Prepare
doesn't do that, and in fact nothing in the Command does. The parameter values are never substituted into the actual command string. It is sent to the DB separately, which is good for several reasons:
- This allows sqlserver (or other dbs) to cache the query plan for your query and reuse it next time. When a different commandtext string is sent to the db each time, the database has to develop a plan each time.
- By sending the parameters to the db compartmentalized, there is a nature defense against sql injection attacks.
Unless you're using a really old database (sql server 7?), .Prepare()
is not necessary and does not actually do anything for you. It used to be helpful in that it would compile the query on the server, but that is done for you automatically now. I haven't use .Prepare()
in a long time.
Hmmmmm. Looking here it seems .Prepare()
does still do something for you: if any values are larger than the parameter defined length, the .Prepare() truncates the value, so that when you execute you don't get the error and the query succeeds. cool.