查询功能:界面上有4个输入框,  每个输入框的值作为查询条件(可以为空)。
4个框的值需要关联查询5个表用的方法太笨了,请教一个简单的方法:
if (textBox1.Text.Trim() != "" && (textBox2.Text.Trim() != "" && textBox3.Text.Trim() != "" && (textBox4.Text.Trim() != "")
{sql  = }
if (textBox1.Text.Trim() != "" && (textBox2.Text.Trim() != "" && textBox3.Text.Trim() != "")
{sql  = }
if (textBox1.Text.Trim() != "" && (textBox2.Text.Trim() != "")
{sql  = }
........................
........................
........................
.................................................

解决方案 »

  1.   

    if (textBox1.Text.Trim() != "")
    {
    sql+=" and xxx ";
    }
    if (textBox2.Text.Trim() != "")
    {
    sql+=" and xxx ";
    }
    ...
      

  2.   


    拼接SQL语句
    if (textBox1.Text.Trim() != "") 

    sql+=" and xxx "; 

    if (textBox2.Text.Trim() != "") 

    sql+=" and xxx "; 

    ...
    [
       
      

  3.   

    string sql = "" ;
    if (textBox1.Text.Trim() != "" ){ sql ="...." }
    if (textBox2.Text.Trim() != "" )

           if(sql=="" ) sql="....." 
           else sql+={.......}
    }
    if (textBox3.Text.Trim() != "" )

           if(sql=="" ) sql="....." 
           else sql+={.......}
    }
    .....
    .....
    .....
      

  4.   

    string sql=".... where 1=1 ";
    if (textBox1.Text.Trim() != "") 

    sql+=" and a.xx"; 

    if (textBox2.Text.Trim() != "") 

    sql+=" and b.xx"; 

    ...
      

  5.   

    string sqlWhere="";
    if (textBox1.Text.Trim() != "") 

    sql+=" and a.xx"; 

    if (textBox2.Text.Trim() != "") 

    sql+=" and b.xx"; 

    ...
    if(!String.IsNullOrEmpty(sqlWhere))
    {
        sqlWhere = sqlWhere.SubString(5);
    }
      

  6.   

    Control[] ctrl = new Control[5] { textBox1, textBox2, textBox3, textBox4 ,.......};
                string strSQL = " insert ...values(....";
                foreach (Control c in ctrl)
                {                if (c.Text.Trim() != "")
                    {
                        strSQL += c.Text.Trim() + ",";
                    }                strSQL += ")";       }不管多少个都不增加代码量。
      

  7.   

    先定义一个boold类型的变量
    bool a=false;
    执行的sql语句
    string sql="select 表名 form "; 
    首先判断第一个
    if(textBox1.Text.Trim() != "")
    {
        sql+="where ...."      条件自己写
           a=true;
    }
    if(textBox2.Text.Trim() != ""){
         if(a==true){
         sql+="id=.."
         }else{
          sql+="where ..."
          a=true;
    }
    {
    if(textBox3.Text.Trim() != ""){
            if(a==true){
         sql+="id=.."
         }else{
         sql+="where...."  
          a=true;   
         }
    }
    if(textBox4.Text.Trim() != ""){
            if(a==true){
         sql+="id=.."
         }else{
          sql+="where ..."       如果是这个意思  请好好看看   
          a=true; 
         }
    }
    如果后面还有可以继续这样写的
      

  8.   


    //如果是AND条件
    string sql="select * from table where ";
    string where="1=1 ";if(textBox1.Text.Trim() != "")
    {
        where+=" AND "+条件1;
    }if(textBox2.Text.Trim() != "")
    {
        where+=" AND "+条件2;
    }...
    sql+=where;//如果是OR条件
    string sql="select * from table where ";
    string where="1=2 ";if(textBox1.Text.Trim() != "")
    {
        where+=" OR "+条件1;
    }if(textBox2.Text.Trim() != "")
    {
        where+=" OR "+条件2;
    }...
    sql+=where;//总之来说,只有一个一个地拼接了
      

  9.   

    /*
    界面上有4个输入框,  每个输入框的值作为查询条件(可以为空)。 
    4个框的值需要关联查询5个表 
    */
    create procedure GetInfo
    (
      @str1 varchar(50),--第一个输入框的值
      @str2 varchar(50),--第2个输入框的值
      @str3 varchar(50),
      @str4 varchar(50)
    )
    select ..................我这样写估计你也不懂
    建议你把表结构发出来
    我帮你写
      

  10.   

    string s1=textbox1.text;
    string s2=textbox2.text;
    string s3=textbox3.text;string sql = "select * from table where (@s1=='' or columnName=@s1) and (@s2=='' or columnName=@s2) and (@s3=='' or columnName=@s3)"然后使用sqlcommand,把sql给commandtext,@s1,@s2,@s3作为sqlcommand的sqlParameter就可以了。
      

  11.   

    刚才写的有点问题,重新改了下。string s1=textbox1.text; 
    string s2=textbox2.text; 
    string s3=textbox3.text; string sql = "select * from table where (@s1 is null or columnName=@s1) and (@s2 is null or columnName=@s2) and (@s3 is null or columnName=@s3)" 然后使用sqlcommand,把sql给commandtext,@s1,@s2,@s3作为sqlcommand的sqlParameter就可以了。