sql 语句进行 like in 参数化,按照正常的方式是无法实现的
我们一般的思维是:
Like 参数:
string strSql = "select * from Person.Address where City like ‘%@add%‘";
SqlParameter[] Parameters=new SqlParameter[1];
Parameters[0] = new SqlParameter("@add", "bre");
In 参数
string strSql = "select * from Person.Address where AddressID in (@add)";
SqlParameter[] Parameters = new SqlParameter[1];
Parameters[0] = new SqlParameter("@add", "343,372,11481,11533,11535,11755,11884,12092,12093,12143");
可是这样放在程序里面是无法执行的,即使不报错,也是搜索不出来结果的,
去网上搜索也没有一个明确的答案,经过反复试验,终于解决这个问题
正确解法如下:
like 参数
string strSql = "select * from Person.Address where City like ‘%‘+ @add + ‘%‘";
SqlParameter[] Parameters=new SqlParameter[1];
Parameters[0] = new SqlParameter("@add", "bre");
in 参数
string strSql = "exec(‘select * from Person.Address where AddressID in (‘[email protected]+‘)‘)";
SqlParameter[] Parameters = new SqlParameter[1];
Parameters[0] = new SqlParameter("@add", "343,372,11481,11533,11535,11755,11884,12092,12093,12143");