有几个checkbox textbox实现任选单选、多选查询组合查询。
下面是我的后台代码
public void bind()
    {
        string strsql;
        strsql = "select * from tb_Equipment where  " + Session["sql"] + " order by id desc";  →→→→ 在这里出错
        SqlConnection strcon = new SqlConnection(System.Configuration.ConfigurationManager.AppSettings["strcon"]);
        strcon.Open();
        SqlDataAdapter sda = new SqlDataAdapter(strsql, strcon);
        DataSet ds = new DataSet();
        sda.Fill(ds, "tb_Equipment");
        GridView1.DataSource = ds.Tables["tb_Equipment"];
        GridView1.DataKeyNames = new string[] { "id" };
        GridView1.DataBind();
    }  protected void Button1_Click(object sender, EventArgs e)
    {
        string Sql = " ";
        if (CheckDropkeyid.Checked)
        {
            Sql = Sql + "and Dropkeyid like '%" + Dropkeyid.Text + "%' ";
        }
        if (CheckDropCompany.Checked)
        {
            Sql = Sql + "and DropCompany like '%" + DropCompany.Text + "%' ";
        }
        if (CheckMakeyid.Checked)
        {
            Sql = Sql + "and Makeyid like '%" + Makeyid.Text + "%' ";
        }
        if (CheckEquipment.Checked)
        {
            Sql = Sql + "and Equipment like '%" + Equipment.Text + "%' ";
        }
        if (CheckBuyDate.Checked)
        {
            Sql = Sql + "and BuyDate between '" + BuyDate1.Text + "' and '" + BuyDate2.Text + "'";
        }
        Session["sql"] = Sql;
        this.bind();
    }

解决方案 »

  1.   

    strsql = "select * from tb_Equipment where  " + Session["sql"] + " order by id desc";  →→→→ 在这里出错SELECT * FROM tb_Equipment 条件 order by id desc
    你的条件就是Session["sql"]吗?
      

  2.   

    当然啦,你把 Session["sql"] = Sql; 这句改成 Session["sql"] = " 1=1" + Sql;因为按你的程序生成的sql是错误的。
      

  3.   

    strsql = "select * from tb_Equipment where 1=1 " + Session["sql"] + " order by id desc";改成这样就可以了
      

  4.   

    ls正解
    where后面不可能直接跟 and的
      

  5.   

    组合查询参考代码:
    create procedure Rp_SY_Detail 
    (
    @ABS_RECEIVE_DATE_BeginDate varchar(100),--申请(开始)时间
    @ABS_RECEIVE_DATE_EndDate  varchar(100),--申请(结束)时间
    @PC_DATE_BeginDate  varchar(100),--上样(开始)时间
    @PC_DATE_EndDate  varchar(100),--上样(结束)时间
    @SH_ID varchar(100),--工人
    @PC_ID varchar(100),--型号
    @ABS_RECEIVE_SHOP varchar(100),--所在位置
    @ABS_SEND_SHOP  varchar(100) -- 机构
    )
    as
    begin
    if(@ABS_RECEIVE_DATE_BeginDate<>'' and  @ABS_RECEIVE_DATE_EndDate<>'')
    begin
    declare @sqlWhere varchar(500)
    set @sqlWhere =''
    declare @sql varchar(8000)
    if(@SH_ID <> '' and @SH_ID <>'-1')
    set  @sqlWhere = @sqlWhere +'and SH_ID = '+''''+@SH_ID+''''
    if(@PC_ID <> ''and @PC_ID <>'-1')
    set @sqlWhere = @sqlWhere + 'and PC_ID= ' + ''''+@PC_ID+''''
    if(@ABS_RECEIVE_SHOP <>'' and @ABS_RECEIVE_SHOP <> '-1')
    set @sqlWhere = @sqlWhere + 'and ABS_RECEIVE_SHOP= ' + @ABS_RECEIVE_SHOP
    if(@ABS_SEND_SHOP <>'' and @ABS_SEND_SHOP <> '-1')
    set @sqlWhere = @sqlWhere + 'and ABS_SEND_SHOP= ' + ''''+@ABS_SEND_SHOP+''''
    if(@PC_DATE_BeginDate<>''and @PC_DATE_EndDate<>'')
    set @sqlWhere = @sqlWhere + 'and PC_DATE> ' + ''''+@PC_DATE_BeginDate+'''' +  'and PC_DATE< ' + ''''+@PC_DATE_EndDate+''''
    set @ABS_RECEIVE_DATE_EndDate = @ABS_RECEIVE_DATE_EndDate +' 23:59:59'
    set @PC_DATE_EndDate = @PC_DATE_EndDate +' 23:59:59' set @sql = 
    'select A.*,B.*,C.*,D.* from  V_ST_APPLY_BILL_SHOWPIECE_HISTORY A
    left join
    (select ORG_ID,ORG_NAME from SYS_ORGANIZATION ) as B
    on ORG_ID = A.ABS_SEND_SHOP
    left join
    (select EM_ID,EM_NAME from EB_MARKET ) as C
    on C.EM_ID = A.ABS_RECEIVE_SHOP
    left join
    (select PC_ID,PC_NAME from SYS_PRODUCT) as D
    on D.PC_NAME= A.PC_NAME
    where ABS_RECEIVE_DATE >'''+@ABS_RECEIVE_DATE_BeginDate+''' and ABS_RECEIVE_DATE < '''+@ABS_RECEIVE_DATE_EndDate+''''


    + @sqlWhere + 'order by ABS_ORDER_ID asc' 
    print (@sqlWhere)
    print(@sql)
    EXEC (@sql)
    end
    end