比如现在有3个select
select1 select2 select3这3个select组成sql查询的where条件,但是这3个select其中一个是可以为空的
那我现在的做法是判断哪个select有选中条件,那么没选中条件,然后拼凑成sql查询
但如果条件多了的话,那if就有很多了...我想问问有没有更好的方法呢

解决方案 »

  1.   

    where  1=1 下面那个需要  就 and 条件10个条件  你肯定要判断10次吧?
      

  2.   

    用三元运算符string sql="select * from tb where 1=1 ";sql+=string.isNullOrEmpty(select1.selectvalue)?"":" and name='"+select1.selectvalue+"'";
    sql+=string.isNullOrEmpty(select2.selectvalue)?"":" and name1='"+select2.selectvalue+"'";
    sql+=string.isNullOrEmpty(select3.selectvalue)?"":" and name2='"+select3.selectvalue+"'";
      

  3.   

    噢,原来可以用where 1=1...懂了