asp.net中like 使用参数化(2009-12-01 15:34:30)

标签:

杂谈

分类: 遇到并解决之SQL

对于 普通的 selectsql语句, 正常的参数化 语句 格式:

          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%'

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

posted on 2012-10-24 09:55  微澜  阅读(272)  评论(0编辑  收藏  举报