Using parameters is convenient, but not the only way to protect yourself from SQL injection and is not reliable.
If a stored procedure internally dynamically creates and executes EXEC () code in SQL, you lose all this protection. You will also lose this protection if you have one request in the application that includes these fields without using parameters or escaping.
Parameters are not magical, and you can protect yourself simply by using the escape function when building SQL:
Public Shared Function StringToSql(ByVal s As String) As String If s Is Nothing Then Return "NULL" Return "N'" & Replace(s, "'", "''") & "'" End Function
Using:
Sql = "INSERT INTO mytable(name) VALUES(" & StringToSql(username) & ")"
Boom. As easy as pie. Even takes care of quoting nvarchar.
You can convert dates, numbers, GUID, etc. into strings and pass them to the above function, but you better create separate functions for each data type used.
There is one caveat: you have to use it every time you create a request — every CRUD that includes this data, whether it is produced at the application level or generated dynamically inside a stored procedure.
But you have to do the same to take advantage of the options! So, anyway, you should change your habits and revise your code. There is no way to avoid this (alleged lime).
source share