我有以下代码:
const string Sql =
@"select distinct [name]
from tblCustomers
left outer join tblCustomerInfo on tblCustomers.Id = tblCustomerInfo.CustomerId
where (tblCustomer.Name LIKE '%@SEARCH%' OR tblCustomerInfo.Info LIKE '%@SEARCH%');";
using (var command = new SqlCommand(Sql, Connection))
{
command.Parameters.AddWithValue("@SEARCH", searchString);
...
}
Run Code Online (Sandbox Code Playgroud)
这不起作用,我也试过这个:
const string Sql =
@"select distinct [name]
from tblCustomers
left outer join tblCustomerInfo on tblCustomers.Id = tblCustomerInfo.CustomerId
where (tblCustomer.Name LIKE @SEARCH OR tblCustomerInfo.Info LIKE @SEARCH );";
using (var command = new SqlCommand(Sql, Connection))
{
command.Parameters.AddWithValue("@SEARCH", "'%" + searchString + "%'");
... …Run Code Online (Sandbox Code Playgroud) 我已经很好地掌握了SQL注入.这是一个SQL查询应该是什么样的
SELECT FirstName, LastName
FROM Customers
WHERE CustomerId = @valueFromApplication
Run Code Online (Sandbox Code Playgroud)
变成了像这样的查询
SELECT FirstName, LastName
FROM Customers
WHERE CustomerId = '' ; DROP DATABASE Foo --
Run Code Online (Sandbox Code Playgroud)
当用户将恶意值插入您的应用程序,网站,客户端等等时......我也知道,攻击者可以尝试发现表的名称并从中获取信息,而不仅仅是删除数据库.
我也知道一些有助于防止这种情况的事情:
这些事情实际上如何防止SQL注入发生?为什么攻击者不能将相同的恶意值传递给他或她已经使用的任何输入并且具有相同的结果.
c# sql entity-framework sql-injection sql-parametrized-query