日期:2014-05-17  浏览次数:21006 次

技术贴!求高手!C#查询sql数据
在C#中查询数据库。但是条件是可变的。where 后面跟的一个或多个条件,
因为条件个数不确定,所以语句不知道怎么写  求高手教我!!!QQ348771523

------解决方案--------------------
string strSQL = "Select * From 表"
if(条件A)
  strSQL += 语句A
else if(条件B)
  strSQL += 语句B
。。。。
------解决方案--------------------
1楼的方法再稍微改进一下更好:

sql = "select * from table where 1=1 ";

sql += " and x = 1 ";
sql += " and y = 'hello' ";
sql += " and z < 20 ";
...

这样,即使where条件一个都没有都不会出错。