我有以下代码:
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) 我必须为我的OJT公司编写应用程序管理系统.前端将在C#中完成,后端在SQL中完成.
现在我从未做过这个范围的项目; 在学校我们只有关于SQL的基础课.不知怎的,我们的老师完全没有讨论SQL注入,我现在才通过在网上阅读它来接触它.
所以无论如何我的问题是:你如何防止C#中的SQL注入?我隐约认为可以通过正确屏蔽应用程序的文本字段来完成它,以便它只接受指定格式的输入.例如:电子邮件文本框的格式应为"example@examplecompany.tld".这种方法是否足够?或者.NET是否有预定义的方法来处理这样的东西?我可以将过滤器应用于文本框,因此它只接受电子邮件地址格式或名称文本框,因此它不接受特殊字符吗?
我有一个WPF应用程序,我得到了
string someone = TextBox.text;
Run Code Online (Sandbox Code Playgroud)
我想在以下查询中使用它
query = " Select * From Table Where Title = someone "
Run Code Online (Sandbox Code Playgroud)
我该如何在查询中使用变量?