sql:
   select * from yourtable where 编号=isnull(@编号,编号)给参数赋值时做一下判断: if(传过来编号的值==string.Empty())
{
    编号参数 = DBNull.Value;
}
else
{
    编号参数=传过来的值;}

解决方案 »

  1.   

    组合查询
    private void BindGridS()
    {
    String sql="select * from mainout where 1=1";
    if(TextBox1.Text!="")

    sql=sql+" and outnumber like @outnumber";
    }
    if(TextBox2.Text!="")
    {
    sql=sql+" and account like @account";
    }
    SqlConnection cn=new SqlConnection(ConfigurationSettings.AppSettings["connectionString"]);
    SqlCommand cm=new SqlCommand(sql,cn);
    cm.Parameters.Add(new SqlParameter("@outnumber",SqlDbType.NVarChar,50));
    cm.Parameters["@outnumber"].Value="%"+TextBox3.Text+"%";
    cm.Parameters.Add(new SqlParameter("@account",SqlDbType.NVarChar,50));
    cm.Parameters["@account"].Value="%"+TextBox4.Text+"%";
    SqlDataAdapter da=new SqlDataAdapter(cm);
    DataSet ds=new DataSet();
    da.Fill(ds);
    DataGrid1.DataSource=ds;
    DataGrid1.DataBind();
    }与此同理
      

  2.   

    sql="select * from mainout where 1=1"   1=1是什么意思?["@outnumber"].Value="%"+TextBox3.Text+"%";     outnumber:不是编号吗,怎么又跟texbox3联系起来了?
    麻烦帮我解释一下,谢谢了
      

  3.   

    同意士人制造,用VIEW是简单的解决办法
      

  4.   

    TO 土人制造:怎么用View来实现这个功能呢??
      

  5.   

    string strSQL="select * from yourtable where 1=1";
    if(编号.text!="")
    {
        strSQL=strSQL+" and 编号字段名='"+编号.text+"'";
    }
    if(名称.text!="")
    {
        strSQL=strSQL+" and 名称字段='"+名称.text+"'";
    }
    以下一样解决。其中1=1是为true ,为以后加and 符号做好充分的准备。明白了吗?
      

  6.   

    你可以做一个通用的吗,例如:
    首先获得要查询的条件,以及所对应的字段,如编号=“”;所属公司编号=“”;
    其次要获得条件的逻辑关系,如符合逻辑关系,则设定条件的关系
    解下来根据条件和逻辑关系生成相应的SQL语句,再执行该就行了。
      

  7.   

    你可以用select * from 数据表 where 字段 like '%"+条件+"%'
      

  8.   

    如果是海量字段+海量数据,用VIEW好象也不咋的。关注中...
      

  9.   

    同意singleflower的意见,做一个通用查询.
      

  10.   

    要组合查询,无非就是字符型精确和模糊查询,数字型大于小于等于,还有不等于等标记。选择要查询的表,增加删除查询条件,拼装每个条件。select语句就是这些的组合。然后根据组合查询结果。
      

  11.   

    不好意思是我写错了,where 1=1是因为如果没有这句的话,就是判断SQL语句是where ...还是and...,如果是第一个条件就用where...如果是后面的连接条件就and...
    如果写上where 1=1就可以把后面的SQL语句统一为and...
    可能说得不清楚,你仔细想想就明白了.
    如果你有固定的where条件,不论哪项组合都要用到同一个条件,就可以用这个条件替换,如果没有固定的一个条件就用where 1=1方便后面一句SQL的统一.
    组合查询
    private void BindGridS()
    {
    String sql="select * from mainout where 1=1";
    if(TextBox1.Text!="")

    sql=sql+" and outnumber like @outnumber";
    }
    if(TextBox2.Text!="")
    {
    sql=sql+" and account like @account";
    }
                 SqlConnection cn=new SqlConnection(ConfigurationSettings.AppSettings["connectionString"]);
                 SqlCommand cm=new SqlCommand(sql,cn);
                 cm.Parameters.Add(new SqlParameter("@outnumber",SqlDbType.NVarChar,50));
                 cm.Parameters["@outnumber"].Value="%"+TextBox1.Text+"%";
                  cm.Parameters.Add(new SqlParameter("@account",SqlDbType.NVarChar,50));
                  cm.Parameters["@account"].Value="%"+TextBox2.Text+"%";
    SqlDataAdapter da=new SqlDataAdapter(cm);
    DataSet ds=new DataSet();
    da.Fill(ds);
    DataGrid1.DataSource=ds;
    DataGrid1.DataBind();
    }
      

  12.   

    string strSQL="select * from yourtable where 1=1";
    你可以为string strSQL="select * from yourtable where id>0";