有两个combobox,一个存stdid,一个存stdscore,两个都有一个all的值,现在我想有一条select语句,如果combobox1.text=="all"呢,就不把这个combobox加入到查询条件里去,否则呢就加一个 where stdid=combobox1.text
该怎么写呢

解决方案 »

  1.   

    string sql="select * from yourtable where 1=1 {0}";
    string subsql="";
    if(combobox1.Text=="all")
     subsql="stdid="+bombobox1.Text;
    sql=string.Format(sql,subsql);
      

  2.   

    //少写了个AND
    string sql="select * from yourtable where 1=1 {0}";
    string subsql="";
    if(combobox1.Text=="all")
     subsql=" and stdid="+bombobox1.Text;
    sql=string.Format(sql,subsql);
      

  3.   

    完全同意二楼观点,同时我觉得可以再简单点:
    string sql="select * from yourtable where 1=1";
    if(combobox1.Text=="all") sql += " and stdid="+bombobox1.Text;
      

  4.   

    select * from yourtable where stdscore=(case stdscore when @score='' then stdscore else @score) and stuid=(case stdid when @id='' then stdid else @id)