如题,我需要把数据库中的数据筛选出来,现在想到的两种办法如图,
一是用IF语句多重循环comboBox中的值来筛选,功能试过能实现,但是代码过于冗长。
二是用dataview来筛选,但是出现BUG,每次都只会按第二个条件筛选,第一个条件没用。单一筛选条件测试过是可以用的。
不知道有什么比较好的方法?
stringbuilder sb = new stringbuilder();
sb.append(" where 1=1 ");
if(comboBox1.Text..ToString()!=""){
sb.append(" and stype like '"+comboBox1.Text..ToString()+"' ");
}
if(comboBox2.Text..ToString()!=""){
sb.append(" and region like '"+comboBox2.Text..ToString()+"' ");
}
string sql = sb.ToString();
不需要多个if
if (comboBox1.Text == "xxx" && comboBox2.Text == "yyy")
...
第二个办法可以用and连接
string filter = "xxx like yyy and zzz like aaa"
这样写不是很合理 。这里的查询,最好重新访问数据库,获取数据 。 因为数据窗口的内容未必是所有的数据。
如果在数据窗口过滤,那就是双列过滤的问题。 同上面解答 。