通过一个查询按钮,点击,然后取出listbox里所有的想作为条件,查询目前的代码是protected void ImageButton7_Click(object sender, ImageClickEventArgs e)
    {
        string[] arr = new string[ListBox4.Items.Count];//ListBox4.Items.Count获得项数
        for (int i = 0; i < ListBox4.Items.Count; i++)//循环取出每项的值
        {
            arr[i] = ListBox4.Items[i].Text;
        }
        foreach (string li in arr)//循环输出每项的值
        .
        .
        .
    }然后比如说string sql="";//如何将数组里每一个li都作为条件比如 select * from 表 where abc='li1' and abc='li2'

解决方案 »

  1.   

    学习了........
    我的想法是先判断数组的length,这样就可以判断出多少个and
    然后根据索引去查....
    但是,不知道怎么实现.
      

  2.   

    SQL也可以通过循环来查询所要的数据
      

  3.   

    那就不需要foreach了么,在for循环直接写
      

  4.   

    string condition =“”
    for循环里面
    condition+= and abc=‘aaa’;
    for外面
    sql+=condition
      

  5.   

    for循环里面
    condition+= and abc=ListBox4.Items[i].Text;
      

  6.   

    比如 select * from 表 where abc='li1' and abc='li2'  这个例子好像永远没有结果..我没大听懂,请问你想实现一个什么功能,是一个数据库字段匹配多个值?还是什么..string strSql = "select * from 表 where 1=1"
    foreach (string li in arr)//循环输出每项的值
    {
       strSql += " and abc='"+li+"'";
    }如果是你比如这个样子的话这个strSql可能就是你想要的语句...
      

  7.   


    protected void ImageButton7_Click(object sender, ImageClickEventArgs e)
        {
            stringbuilder sb = new stringbuilder("select * from tablename where 1=1 ");
            for (int i = 0; i < ListBox4.Items.Count; i++)//循环取出每项的值
            {
                sb.append(" and abc = '" + ListBox4.Items[i].Text + "'");
            }
           //GoSql(sb.ToString());    
        }
      

  8.   

    string sql="select * from tb where 1=1";
    string str="";
    for(int i=0;i<ListBox1.Items.Count;i++ )
      {
        str+=" abc='"+ ListBox1.Items[i].Text)+"' or ";
      }  
    str=str.EndsWith("or")?str.SubString(0,str.length-2):"";
    sql+="("+str+")";
      

  9.   

    试试吧。
          string strWhere = string.Empty;
          for (int i = 0; i < ListBox4.Items.Count; i++)
          {
            strWhere += string.Format("'{0}',", ListBox4.Items[i].Text);
          }
          strWhere = strWhere.Substring(0, strWhere.Length - 1); 
          string strSql = string.Format("select * from 表 where abc in ({0}) ", strWhere);