Taking over some code from my predecessor and I found a query that uses the Like operator:
SELECT * FROM suppliers WHERE supplier_name like '%'+name+%';
Trying to avoid SQL Injection problem and parameterize this but I am not quite sure how this would be accomplished. Any suggestions ?
note, I need a solution for classic ADO.NET - I don't really have the go-ahead to switch this code over to something like LINQ.
Short Anwser:
1) name.Replace("'", "''").... Replace any escape characters that your database may have (single quotes being the most common)
2) if you are using a language like .net use Parameterized Queries
The above gets replaced with the below
3) user Stored procs
4) use Linq to SQL, again if you are using .net
In Entity Framework 6 it could be done like this by Native SQL:
Or
Also, you can just use LINQ to Entities directly:
Simply parameterize your query:
Now you can pass your "name" variable into the @name parameter and the query will execute without any danger of injection attacks. Even if you pass in something like "'' OR true --" it'll still work fine.
try this:
the framework will automatically deal with the quoting issues.