日期:2014-05-19  浏览次数:20472 次

SQL参数查询的问题,如何用参数方式构成in 条件的查询
比如最终SQL语句是
select   *   from   mytable   where   id   in   (1,3,5,6)

in的集合是不确定的。
如果现在我的1,3,5,6这个集合取得已经放在arraylist中了。
如何用参数形式而不是string+string的方式来构成这个查询?


------解决方案--------------------
str等于arraylist的值的集合 //保持 n1,n2,n4这样的
然后
select * from mytable where id in ( " + str + ")

------解决方案--------------------
好象就得用string这个东西进去 然后在里面分割字符串
MARK一下 等待高手解决
------解决方案--------------------
string strSql = string.Empty;
string strCollection = string.Empty;
strCollection = "( ";
foreach(int iItem in arraylist)
{
strCollection += iItem + ", ";
}
strCollection = strCollection.Substring(0, strCollection.Length - 1) + ') '

strSql = "select * from mytable where id in " + strCollection;
------解决方案--------------------
可以用分隔符把字符串连起来传到存储过程中,然后写一个把这个字符串分开成表的函数,用你的数据库表和这个表进行关联关联查询而不用IN操作,实际上IN操作不太建议使用.