if(ad!=null|ad!=''){
string str="select * from A where A_id=4 and a_name="+ad}else{
select * from A where A_id=4 
}
 要是where后面有在多的参数 ,x,y,z就得写更多的if因为要判断他们是否为空,
有什么解决办法吗

解决方案 »

  1.   

    string str="select * from A where 1=1";
    if(...)
       str += ...
      

  2.   

    不知道是否要的这种效果string strSql = "select * from A where A_id=4";
    if(!string.IsNullOrEmpty(x)) {
       strSql += " and x="+ x ;
    }if(!string.IsNullOrEmpty(y)) {
       strSql += " and y="+ y ;
    }
    如果不想这么麻烦,可以定义一个实体,通过反射机制来做!
      

  3.   

    上面只是随便写的DEMO,没有注意细节,不要喷哦,^_^