我有10几个TextBox的查询,当TextBox内容为空时,我想将其滤掉
但是在sql里的条件怎么写
例:select * from 表 where 列1=TextBox1.Text and 列2 =  TextBox2.Text
....
如果TextBox2为空时 那么条件 列2 =  TextBox2.Text 就不要了了这段sql应该怎么写好

解决方案 »

  1.   

    declare @sql varchar(8000)
    set @sql='select * from T where 1=1 '
    if TextBox1.Text<>""
      set @sql=@sql+' and 列1='''+TextBox1.Text+''' '
    if TextBox2.Text<>""
      set @sql=@sql+' and 列2='''+TextBox2.Text+''' '
    ...exec(@sql)--引號問題自己注意下
      

  2.   

    string sql="select * from 表 where 1=1 ";
    if TextBox1.Text =="" || TextBox1.Text ==null
        sql+=" and 列1='"+TextBox1.Text+"'";
      

  3.   

    这个在程序中拼sql语句就行了嘛dim iSQL as string
    iSQL = "select * from 表 where 1=1"
    iSQL = iSQL & iif(TextBox1.Text = "", "", " and 列1='" & TextBox1.Text & "'")
    iSQL = iSQL & iif(TextBox2.Text = "", "", " and 列1='" & TextBox2.Text & "'")
    .....
    iSQL = iSQL & iif(TextBox10.Text = "", "", " and 列1='" & TextBox10.Text & "'")这样可以拼出一个sql语句 , 然后用command对象执行这个语句就可以了.
      

  4.   

    写错了
    string sql="select * from 表 where 1=1 ";
    if TextBox1.Text <>"" && TextBox1.Text <>null
        sql+=" and 列1='"+TextBox1.Text+"'";
      

  5.   

    两种方法,
    一种用动态SQL写,为空就不要这个条件不为空就+' and 条件'
    另一种用(条件 or 参数 is null) and ...
      

  6.   

    VB:
    dim sql as string
    dim flag as boolean
    sql="select * from 表"
    flag=false
    if trim(TextBox1.Text)<>"" then
       sql=sql & " where 列1=" & TextBox1.Text 
       flag=true
    end ifif trim(TextBox2.Text)<>"" then
       if flag=false then
           sql=sql & " where 列2=" & TextBox2.Text 
       else
           sql=sql & " and 列2=" & TextBox2.Text 
       flag=true
    end ifif trim(TextBox3.Text)<>"" then
       if flag=false then
           sql=sql & " where 列3=" & TextBox3.Text 
       else
           sql=sql & " and 列3=" & TextBox3.Text 
       flag=true
    end if...
      

  7.   

    select * from 表 where 列1=isnull(nullif(TextBox1.Text, ''), 列1) and 列2=isnull(nullif(TextBox2.Text, ''), 列2)
      

  8.   

    select * from table_name where column1_name like '%'+TextBox1.Text+'%' and
    column2_name like '%'+TextBox2.Text+'%' and column3_name like '%'+TextBox3.Text+'%'like 就可以了。
      

  9.   

    case TextBox1.Text <> null then NEXT另 :可以在程序内实现啊,,毕竟比SQL执行得快点点吧,
    str sql = "select * from 表 where 1=1"
    if (TextBox1.Text != NULL)
        strsql =str + "  and" +"列1=TextBox2.Text"