Of course, the first step to prevent SQL Injection attacks is to always use parameterized queries, never concatenate the text provided by the client into an SQL string. Using stored procedures does not matter if you take the step to parameterize.
However, there is a secondary source of SQL injection where the SQL code itself (usually in SP) will need to be some SQL, which is then EXEC'd. Therefore, it can still be useful for injection, although your ASP code always uses parameterized queries. If you can be sure that none of your SQL does this and will never do it, then you are safe enough from SQL Injection. Depending on what you are doing and which version of SQL Server you are using, there are times when SQL SQL compilation is inevitable.
Based on the foregoing, a robust approach may require that your code examine incoming string data for SQL templates. This can be quite intense work, because attackers can become quite complex in preventing the discovery of SQL patterns. Even if you feel that the SQL you are using is unsafe, it is useful to be able to detect such attempts, even if they fail. The ability to select and record additional information about the HTTP requests that attempt is good.
Escaping is the most reliable approach, in this case all code that uses the data in your database must understand the transition mechanism and be able to cancel the data in order to use it. Imagine, for example, a server-side reporting tool, you will need to use the unescape database fields before including them in the reports.
Server.HTMLEncode prevents a different form of injection. Without it, an attacker could inject HTML (including javascript) into the output of your site. For example, imagine a storefront application that allows customers to view products for other customers to read. A malicious “client” might add some HTML code that could allow them to collect information about other real clients who read the “review” of a popular product.
Therefore, it always uses Server.HTMLEncode for all string data received from the database.
source share