现在的位置: 首页 > 综合 > 正文

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

2012年10月25日 ⁄ 综合 ⁄ 共 764字 ⁄ 字号 评论关闭
 
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%'

不会报错,不过你不可能查询到想要的结果.

抱歉!评论已关闭.