请问我想要在5,6个约束条件的情况下绑定gridview,因为有5,6个约束条件,因此我不想根据字段的空或者不空写很多的select语句,我想传参数的时候,把空与不空的值都传给select语句中的where子句,这样的话就只需要写一条select语句就能全部解决了!但是如何在字段为空的时候,写出where 字段名=“全体值”的这种情况?如果可以的话,我可以先用if语句赋变量为全体值!一起传值过去。
之前想的是where 字段名=*,但是不能通过。
哪位高手能给解决下哈?谢谢了!

解决方案 »

  1.   

    string sql = "select * from tab where 1=1 ";if(name != "")
    {
    sql += " and name='"+name+"'";
    }
    //.....
      

  2.   


    UP  注意  and前面的空格
      

  3.   

    楼主,你要是不怕效率低的话,可以把name='' 换成 name='%这里是你传递进来的值%',其他条件也一样
      

  4.   

    不好意思,打错了name like '%这里是你传递进来的值%'
      

  5.   

    字段为空的时候,不加这个条件.if(查询值!="")
    {
     strSQL += "这个条件"}
      

  6.   

    数据库写个存储
        
    create proc p_selectBy
    @where1 varcahr(5),
    @where2 varcahr(5),
    @where3 varcahr(5),
    ...
    as
    declare @sqlwhere varchar(5000)
    set @sqlwhere ='1=1'
    if(@where1 is not null && @where1 <>'')
       set @sqlwhere =@sqlwhere +' and column1='+@where1
    if(@where2 is not null && @where1 <>'')  set @sqlwhere =@sqlwhere +' and column2='+@where2
    if...
      

  7.   

    string sql = "select * from tab where 1=1 ";if(name != "")
    {
    sql += " and name='"+name+"'";
    }
    这个不错