SQL 参数化查询 应用于 Like

  在sql 进行参数化查询的时候,使用like 语句和参数的时候,错误的写法:  Participant like '%@Participant%' ,这样在数据库为解析为 '%'participant'%',而不是作为变量的形式加入进去。

 

解决办法一:

使用连接的方式

Participant like '%'+@Participant+'%'

SqlParameter[] sps ={
  new SqlParameter("@Participant",SqlDbType.VarChar){Value=participant},

}

 

 

解决办法二:

使用连接的方式

Participant like '@Participant'

参数的只中添加 %%

SqlParameter[] sps ={
  new SqlParameter("@Participant",SqlDbType.VarChar){Value="%"+participant+"%"},

}

 

posted @ 2015-05-22 16:04  Young汨  阅读(642)  评论(0编辑  收藏  举报