这样吧,用for读出选中的,放在数组中,
然后判断数组个数,得出是单选还是双选,
然后,相关查询就可以了

解决方案 »

  1.   

     我感觉这样写比乘法口诀表都复杂的多,(而且光300个单独判断就要很多代码)
    我不这样认为啊,你判断它选中的个数,如果一个,就单个查询,如果是二个,就查询,如果是三个,得到三个的值,就按三个查询。
    用for查询,代码简单,取值方便
    for(int i=0; i<300,i++)
    {
    //有没有选中,有就传出值(i)
    }
      

  2.   


    按照你那个写法 ,比如返回是1,5 或者15,25,35...
     判断应该怎么判断? 我想的是:2个参数SQL的条件就是 X and X,3个就是x adn x and x ,.....300个以此类推
     SQL语句写的时候该怎么写呢?
      

  3.   

            string st=new string [];
            for (int i = 0; i < 300; i++ )
            {
                if (CheckBox1.Checked)
                {
                    int i = 0;
                    st[i]=CheckBox1.Text.ToString();
                    i=i+1;
                }
            }
            int int_st=st.Length;
            int_st是多少就为多少查询
            查询的值就是数值那儿,要用就调了出来
      

  4.   

            int int_st=st.Length;
            for(int i=0;i<st.Length;i++)
            {
                string a=st[i];
                a=a+"and"+ st[i];
            }
    类似这样子,得到字符串,具体,你仔细想想,由于工作关系,不能细想,写出来,楼主自己想相
      

  5.   


    大哥 你这个代码 返回的是传过来多少个参数, 你意思是不是是,返回一个数组,再取出数组的值,把数组取出的值传进SQL语句?
     
     那SQL应该怎么写呢??? 我以前写过一个5个参数的,先new 一个选择框
    foreach循环读取数据绑定选择框
    最后在用foreach读取已经选择的框。。
    ,因为可能出现的情况太多了,
     5个参数我判断+拼SQL就用了150行代码呢,现在我有300多个参数呢~~~~
      

  6.   

    把你的多个checkbox做成控件数组,用循环来依次检测,然后再生成查询条件. 
      

  7.   

      这个是我以前写的5个参数的部分判断,这个能改进么·····
      还是这个改进后就是15楼的那个循环?
     if (jid == -1 && xid == -1 && rid == -1 && verifyid == -1 && shpid == -1)
            {
                string sql = "select * from JW_4_4 order by id desc";
                Bind(sql, con);
            }
            if (jid == -1 && xid != -1 && rid == -1 && verifyid == -1 && shpid == -1)
            {
                string sql = "select * from JW_4_4 where x_id=" + xid + "  order by id desc";
                Bind(sql, con);
            }
            if (jid == -1 && xid == -1 && rid != -1 && verifyid == -1 && shpid == -1)
            {
                string sql = "select * from JW_4_4 where r_id=" + rid + "  order by id desc";
                Bind(sql, con);
            }
            if (jid == -1 && xid == -1 && rid == -1 && verifyid != -1 && shpid == -1)
            {
                string sql = "select * from JW_4_4 where verifyid=" + verifyid + "  order by id desc";
                Bind(sql, con);
            }
            if (jid == -1 && xid == -1 && rid == -1 && verifyid == -1 && shpid != -1)
            {
                string sql = "select * from JW_4_4 where shpid=" + shpid + "  order by id desc";
                Bind(sql, con);
            }
            if (jid != -1 && xid == -1 && rid == -1 && verifyid == -1 && shpid == -1)
            {
                string sql = "select * from JW_4_4 where j_id=" + jid + " order by id desc";
                Bind(sql, con);
            }
            if (jid != -1 && xid != -1 && rid == -1 && verifyid == -1 && shpid == -1)
            {
                string sql = "select * from JW_4_4  where j_id=" + jid + " and x_id=" + xid + " order by id desc";
                Bind(sql, con);
            }
            if (jid != -1 && xid == -1 && rid != -1 && verifyid == -1 && shpid == -1)
            {
                string sql = "select * from JW_4_4  where j_id=" + jid + " and r_id=" + rid + " order by id desc";
                Bind(sql, con);
            }
            if (jid != -1 && xid == -1 && rid != -1 && verifyid != -1 && shpid == -1)
            {
                string sql = "select * from JW_4_4 where j_id=" + jid + " and r_id=" + rid + " and verifyid=" + verifyid + " order by id desc";
                Bind(sql, con);
            }
            if (jid != -1 && xid == -1 && rid == -1 && verifyid != -1 && shpid == -1)
            {
                string sql = "select * from JW_4_4 where j_id=" + jid + "  and verifyid=" + verifyid + " order by id desc";
                Bind(sql, con);
            }
            if (jid != -1 && xid == -1 && rid != -1 && verifyid != -1 && shpid != -1)
            {
                string sql = "select * from JW_4_4 where j_id=" + jid + " and r_id=" + rid + " and verifyid=" + verifyid + " and shpid=" + shpid + " order by id desc";
                Bind(sql, con);
            }
            if (jid != -1 && xid != -1 && rid != -1 && verifyid == -1 && shpid == -1)
            {
                string sql = "select * from JW_4_4  where j_id=" + jid + " and x_id=" + xid + " and r_id=" + rid + "  order by id desc";
                Bind(sql, con);
            }
            if (jid != -1 && xid != -1 && rid != -1 && verifyid != -1 && shpid == -1)
            {
                string sql = "select * from JW_4_4  where j_id=" + jid + " and x_id=" + xid + " and r_id=" + rid + " and verifyid=" + verifyid + "  order by id desc";
                Bind(sql, con);
            }
            if (jid != -1 && xid == -1 && rid == -1 && verifyid != -1 && shpid != -1)
            {
                string sql = "select * from JW_4_4  where j_id=" + jid + "  and verifyid=" + verifyid + " and shpid=" + shpid + " order by id desc";
                Bind(sql, con);
            }
            if (jid != -1 && xid == -1 && rid == -1 && verifyid == -1 && shpid != -1)
            {
                string sql = "select * from  JW_4_4 where j_id=" + jid + "  and shpid=" + shpid + " order by id desc";
                Bind(sql, con);
            }
    //
            if (jid != -1 && xid != -1 && rid != -1 && verifyid != -1 && shpid != -1)
            {
                string sql = "select * from JW_4_4  where j_id=" + jid + " and x_id=" + xid + " and r_id=" + rid + " and verifyid=" + verifyid + " and shpid=" + shpid + " order by id desc";
                Bind(sql, con);
            }
            if (jid != -1 && xid != -1 && rid == -1 && verifyid != -1 && shpid != -1)
            {
                string sql = "select * from JW_4_4  where j_id=" + jid + " and x_id=" + xid + "  and verifyid=" + verifyid + " and shpid=" + shpid + " order by id desc";
                Bind(sql, con);
            }
            if (jid != -1 && xid != -1 && rid == -1 && verifyid == -1 && shpid != -1)
            {
                string sql = "select * from JW_4_4  where j_id=" + jid + " and x_id=" + xid + " and shpid=" + shpid + " order by id desc";
                Bind(sql, con);
            }
            if (jid != -1 && xid != -1 && rid == -1 && verifyid != -1 && shpid == -1)
            {
                string sql = "select * from  JW_4_4 where j_id=" + jid + " and x_id=" + xid + "  and verifyid=" + verifyid + "  order by id desc";
                Bind(sql, con);
            }
            if (jid != -1 && xid != -1 && rid != -1 && verifyid == -1 && shpid != -1)
            {
                string sql = "select * from JW_4_4  where j_id=" + jid + " and x_id=" + xid + " and r_id=" + rid + " and shpid=" + shpid + " order by id desc";
                Bind(sql, con);
            }
            if (jid != -1 && xid == -1 && rid != -1 && verifyid == -1 && shpid != -1)
            {
                string sql = "select * from JW_4_4  where j_id=" + jid + "  and r_id=" + rid + "  and shpid=" + shpid + " order by id desc";
                Bind(sql, con);
            }
            if (jid == -1 && xid != -1 && rid != -1 && verifyid != -1 && shpid != -1)
            {
                string sql = "select * from JW_4_4  where  x_id=" + xid + " and r_id=" + rid + " and verifyid=" + verifyid + " and shpid=" + shpid + " order by id desc";
                Bind(sql, con);
            }
            if (jid == -1 && xid == -1 && rid != -1 && verifyid != -1 && shpid != -1)
            {
                string sql = "select * from JW_4_4  where  r_id=" + rid + " and verifyid=" + verifyid + " and shpid=" + shpid + " order by id desc";
                Bind(sql, con);
            }
            if (jid == -1 && xid == -1 && rid == -1 && verifyid != -1 && shpid != -1)
            {
                string sql = "select * from JW_4_4  where  verifyid=" + verifyid + " and shpid=" + shpid + " order by id desc";
                Bind(sql, con);
            }
            if (jid == -1 && xid != -1 && rid != -1 && verifyid == -1 && shpid == -1)
            {
                string sql = "select * from JW_4_4  where x_id=" + xid + " and r_id=" + rid + "  order by id desc";
                Bind(sql, con);
            }
            if (jid == -1 && xid != -1 && rid != -1 && verifyid != -1 && shpid == -1)
            {
                string sql = "select * from JW_4_4  where  x_id=" + xid + " and r_id=" + rid + " and verifyid=" + verifyid + " order by id desc";
                Bind(sql, con);
            }
            if (jid == -1 && xid == -1 && rid != -1 && verifyid != -1 && shpid == -1)
            {
                string sql = "select * from JW_4_4  where  r_id=" + rid + " and verifyid=" + verifyid + " order by id desc";
                Bind(sql, con);
            }
            if (jid == -1 && xid != -1 && rid == -1 && verifyid != -1 && shpid == -1)
            {
                string sql = "select * from JW_4_4  where  x_id=" + xid + " and verifyid=" + verifyid + " order by id desc";
                Bind(sql, con);
            }
            if (jid == -1 && xid == -1 && rid != -1 && verifyid == -1 && shpid != -1)
            {
                string sql = "select * from JW_4_4  where  r_id=" + rid + " and shpid=" + shpid + " order by id desc";
                Bind(sql, con);
            }
            if (jid == -1 && xid != -1 && rid == -1 && verifyid == -1 && shpid != -1)
            {
                string sql = "select * from JW_4_4  where  x_id=" + xid + " and shpid=" + shpid + " order by id desc";
                Bind(sql, con);
            }
    求建议~
      

  8.   


    谢谢给我建议,不不知道SQL语句的条件怎么生成麻烦说下 好么~~~~
      

  9.   

    那个语句是不是:
    一个参数的
    string sql=null;
    if(checkbox1.checked){
     
    sql="select * from Table1 where 参数=1 ";
      
    }
    其他一个的参数一样类似...
    二个参数的判断
    if(checkbox1.checked&& checkbox2.checked){  sql="select * from Table1 where 参数=1 and 参数=2";
    }
    以此类推 还是跟我那个想法一样我感觉语句只能这样写,有没有简单的办法?
      

  10.   

    选择参数的时候 根据选择中的参数 生成 SQl中的一个部门 然后直接 就查询。
    example:字典dictionary{key:参数一,value:"file";key:参数二 ,value:"name"};sql语句条件部门 sqlstring;
    //每次选择参数都会触发
    选择参数事件
    {
    这里用一个list<dictionary[key].value>记录选择过的参数
    if(list.contain(key)
    {
     删除sql语句中一个部分的方法(dictionary[key].value)
    }
    else
    {
     key 通过选择参数穿过来 肯定可以得到选择的参数的名字
      组合sql语句方法(dictionary[key].value)
    list.add(dictionary[key].value);
    }
      
    }
    组合sql语句方法(dictionary.value)
    {
    //组合逻辑
    sqlstring += dictionary.value
    }
    删除sql语句方法(dictionary.value)
    {
    //删除逻辑
    sqlstring += dictionary.value
    }
      

  11.   


      int int_st=st.Length;
      for(int i=0;i<st.Length;i++)
      {
      string sql="select * from Table1 where 参数=1 "
    sql=sql+“and”+st[i]; }
    这样子for输出一个字符串语句
      

  12.   

    string varstrings=string.empty;
    foreach(checkbox c in page.controls)
    {
    if(c.checked)
    {
    varstring+="c条件";
    }
    }
    用varstrings查询
      

  13.   

    这些复选框应该是个CheckBoxList控件吧,如果是,那么用  public static String[] GetSelectedValues(ListControl listCtrl)
            {
                List<String> values = new List<string>();            foreach (ListItem item in listCtrl.Items)
                {
                    if (item.Selected) values.Add(item.Value);
                }            return values.ToArray();
            }
    获取到所有的选中的框后,再根据选中框的value来对应你的参数,至于如何将你的checkbox和参数之间作映射,看实际情况了
      

  14.   


    那样循环出来后 SQL语句是不是这样写:取出数组选中的,进行判断,省略了~~
    string sql=null;
    if(checkbox1.checked){
     
    sql="select * from Table1 where 参数=1 ";
       
    }
    其他一个的参数一样类似...
    二个参数的判断
    if(checkbox1.checked&& checkbox2.checked){  sql="select * from Table1 where 参数=1 and 参数=2";
    }
    以此类推 还是跟我那个想法一样我感觉语句只能这样写,有没有简单的办法?
      

  15.   

    如果你用的是in的话,

    String sql = "select * from tb where id in ("+ String.Join(",",GetSelectedValues(listCheckBox)) + ")";
      

  16.   

    设置checkbox1.Value="参数=1"
    checkbox2.value="参数=2"
    checkbox3.value="参数=300"
    那么就是
    String sql = "select * from tb where ("+ String.Join(" and ",GetSelectedValues(listCheckBox)) + ")";
      

  17.   

    不知道能不能用checkListBox?
      

  18.   

    应该是,那个ListControl是个CheckBoxList
      

  19.   

    前台的的三百多个空间ID都写为名称+序号,比如check_1~check_300,用for循环遍历选中的控件在把序号取出来,然后借助于二维数组(提前写一个二维数组的查询方法)例如obj[0][2]取出列名,因此要求封装的时候将数据库字段名和封装的名字保持一致,在把字段名写入sql语句执行查询。
      

  20.   

     个人认为   建一张表  存 条件字段(filedname)和值(value) 用CheckBoxList 绑定显示
    你不可能 在 前台拖放 300个控件 慢慢弄吧 ,费时费力, 不好管理
        <asp:CheckBoxList ID="CheckBoxList1" DataTextField="FiledName" DataValueField="Value" runat="server">
        </asp:CheckBoxList>   <%
        <asp:CheckBoxList ID="CheckBoxList1" DataTextField="FiledName" DataValueField="Value" runat="server">
        <asp:ListItem Text="jid" Value="jid=jid" ></asp:ListItem>
        <asp:ListItem Text="sid" Value="sid=sid" ></asp:ListItem>
        </asp:CheckBoxList>%>
      

  21.   

    第一次看到这个需求where后面根三百个条件这个不止查询条件多。。有这么多条件。。那这个表也大得狠吧。。你用一个最笨的方法先查一查看看。看要多少时间?如果是靠选择只能想到用for循环不断追加条件
      

  22.   


    或者建一张表 
    ID Value 查询条件
    前台 Value:<input type="checkbox" name="NAME相同" value="ID">
    获取 ID字符串,写个存储过程,拆分ID字符串,拼接每个ID对应的条件,存储过程中获取返回记录集,这样效率会高些吧
      

  23.   

    1.在你需要查询的表里事先需要预先处理,比如第一条记录满足1,2,4,6条件,对应的110101为第一条记录的代码,
    2.在查询页面,选择1,3,5,选择项目,得到对应 代码101010,与110101做AND运算,得到100000,说明满足条件1,只要运算中间有1就行,说明这条记录是满足你查询的条件的,因为你是或的关系,只要满足一个条件就行。如果是且的关系可以用其他的运算,再大的记录数量也不会速度很慢,主要扫描1次表结构。
      

  24.   

    1.首先循环读取出你的参数显示在页面上
    2.然后勾选你要对比的参数选项
    3.用js获取你选中的checkbox   每个checkbox的value ="对比的参数"
    4.获取选中的checkbox的值
      

  25.   

    控件太多了,用户体验不好
    通过列表绑定字段,通过下拉框选择条件=,>,<,!=等,或通过JS添加条件
    实现多条件查询
    string sql="";
    sql+=条件?"":"";
      

  26.   

    其实如梦 说的  拼接 SQL 语句是个不错的方法 用户体验和 访问的速度 都会 好一点
      

  27.   

    只是一个拼sql的过程吧。选中就 + and 。没有选就continue。然后执行sql。不知道说的对不对。