我有10个文本框..按条件查询. 
然后有一个方法把这些条件传入SQL语句进行查询,用户在文本框输入任何一个查询条件,我就要查询一次!!不填则全查出来这样我要写好多查询语句。有没有精炼的查询办法。。最好能写给偶看下。谢谢
数据库是SQL2005

解决方案 »

  1.   

    where 1=1后面+你的条件,有就+没就不+where 1=1 and name = ? and sex = ?这样串起来
      

  2.   

    在程序中通过程序来判断
    if(a 有值)
    {
      a字段='值'
    }
    ...拼接成一条语句,不过要考虑完全性方面的问题。替换一些关键字(如单引号等),最好就是用参数数组的形式。
      

  3.   


    DECLARE @VSQL VARCHAR(6000),
    @TJ1 VARCHAR(100),
    TJ2 VARCHAR(100),
    SET @VSQL = 'SELECT COL1,COL2 FROM TNAME WHERE COL1 LIKE '''+'%'+'''+@TJ1+''' AND 
    COL2 LIKE '''+'%'+'''+@TJ2+''' EXEC(VSQL)
      

  4.   


    有个个好方法 ,语句这样写select *from tb where 
    col1 like textbox1.text and 
    col2 like textbox2.text and 
    col3 like textbox3.text and 
    col4 like textbox4.text and 
    col5 like textbox5.text  在判断textbox若是空的话,则赋予textbox为'%'
     
      

  5.   

    文本框。什么语言。在写sql语句 时,判断文本框的内容就行了
      

  6.   

    直接模糊查询不就好了吗,
    @mhxx --模糊消息
    where col1 like '%'+ @mhxx +'%' or 
          col2 like '%'+ @mhxx +'%' or
          col3 like '%'+ @mhxx +'%' ………………  
      

  7.   

    stringBuilder str="select * from tableName ";
    int n=str.IndexOf("where");
    if(txtID.text!="")
    {
        if(n==-1)str.Append("where id=txtID.text ");
        else str.Append("adn id=txtID.text ");
    }
    if(txtNumber.text!="")
    {
        if(n==-1)str.Append("where number=txtNumber.text ");
        else str.Append("and number=txtNumber.text ");
    }
    ……