首页 > Python基础教程 >
-
C#教程之SQL Server参数化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 ('+@add+')')";
SqlParameter[] Parameters = new SqlParameter[1];
Parameters[0] = new SqlParameter("@add", "343,372,11481,11533,11535,11755,11884,12092,12093,12143");