Inserting into DB with parameters safe from SQL injection? Inserting into DB with parameters safe from SQL injection? asp.net asp.net

Inserting into DB with parameters safe from SQL injection?


Your code is fine, it is protected from injection because the values are passed as parameters not string literals. However, if you are writing this type of data access yourself, have you considered creating SqlParameter objects and explicitly setting the type, size etc, and adding the parameters to the command? AddWithValue will work just fine, but SQL Server will have to determine the type, a little, but unnecessary overhead.


Well, you could always try to inject a SQL statement into the textbox, that will probably give you a quicker, definite answer.


Yes, that's reasonably safe. So long as you don't use "sanitized" variables from a prepared statement to generate dynamic sql later, you're usually ok. The fact that you're using a prepared statement will take care of dealing with escape characters and other simple methods of injection.

I wouldn't forgo any other validation though...