页面上有三个dropdownList控件,可以任意选择,如:
3个全选,
3个都不选,
只选1个,
选择任意两个。页面查询操作,需要根据3个下拉列表的选择进行查询,如:
select * from table where 字段1=dropdownList1 and 字段2=dropdownList2 and 字段3=dropdownList3。
但是页面上也可能没有选择任何一个下拉列表,此时sql就的是
select * from table 。如果这样,根据3个dropdownList控件的选择情况,查询操作需要写8个sql语句。太繁琐了。目前想到的是根据dropdownList控件的选择状态,用StringBuilder去拼Sql语句,但还是感觉不尽如人意,容易拼写出错。
请教各位有什么好的解决办法么?

解决方案 »

  1.   


    根据dropdownList组组合字符串
    string sql = "";
    if (dropdownList1.Text <> "")
    {
    }
    if (dropdownList2.Text <> "")
    {
    }
    依次类推
    如果都没有选择,不组合条件
      

  2.   

    你可以这样
    if(dropdownlist选中)
    {
        sql+=.......
    }
      

  3.   

    需要根据3个下拉列表的选择进行查询。用拼装sql语句吧,但楼主说感觉不尽如人意,
    其实我感觉挺好的。
      

  4.   


    StringBuilder sb=new StringBuilder("select * from table where 1=1 ");
    if(dropdownList1.SelectIndex!=-1)
    {
       sb.Append(" and 字段1=dropdownList1");
    }
    if(dropdownList2.SelectIndex!=-1)
    {
       sb.Append(" and 字段2=dropdownList2");
    }
    if(dropdownList3.SelectIndex!=-1)
    {
       sb.Append(" and 字段1=dropdownList3");
    }
      

  5.   


    string sql = "select * from table where (1=1) ";
    string sqlWhere="";
    if (dropdownList1.Text <> "")
    {
      sqlWhere+=" and 字段1= dropdownList1";
    }
    if (dropdownList2.Text <> "")
    {
      sqlWhere+=" and 字段2= dropdownList2";
    }
    if (dropdownList3.Text <> "")
    {
      sqlWhere+=" and 字段3= dropdownList3";
    }绑定的时候就bindData(sql+sqlWhere)