转 C# , ASP.Net 中 关于 like in 实现参数化查询的问题

 
C# , ASP.Net 中 关于 like in 实现参数化查询的问题。2008-09-18 18:17对于 普通的 select等sql语句, 正常的参数化 语句 格式:

          select * from profile where EmployeeID= @EmployeeID


   for example:


     string loginString = "select * from profile where EmployeeID= @EmployeeID";


but please attention to the like sql   sentence:      

       select * from profile where EmployeeID Like ‘%’ + @EmployeeID + ‘%’;   

   The accurate search format is :

       Select * from profile where EmployeeID like +@EmployeeID ;

So the

        String   = "SELECT   * from   Box   WHERE   BoxID   like   '%'   +   @subString   +   '%'"


对本文提供了有价值的文章有:

c# sql like 参数

2008-08-08 10:09

参数化的意义在于把对应的值从参数中提供,对于like语句,like后面的值则包括了单引号中的所有部分,包括百分号(%),因此在参数化like对应的值时,应该把百分号移到参数值中提供,像这样:

Cmd.Parameters["@KeyWord"].Value = "%" + StrKeyWord + "%";

可别奢想在sql语句中像这样的样子:

Select * From [TableName] Where [Column1] like '%@KeyWord%'

不会报错,不过你不可能查询到想要的结果.
作者:BobLiu
邮箱:lzd_ren@hotmail.com
出处:http://www.cnblogs.com/liuzhendong
本文版权归作者所有,欢迎转载,未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
原文地址:https://www.cnblogs.com/liuzhendong/p/1616180.html