模糊查询简单分析,得出sql语句
最近搞了一个登记系统,到了查询页面时候,有多的DropDownList控件,DropDownList里面很多选择可以选择,就是条件查询.但如何获得该查询的sql语句呢,我本来想到用 case,但考虑到与以前查询具体分数不同,它这里是有模糊查询的概念,譬如,当DropDownList里面有"全部"一项时,那么,case要分很细小,还是不妙啊,真实情况是
DropDownList3里面有 选择项 (全部,1,2,3,4,5) DropDownList4里面有选择项(全部,a,b,c,d,e)那么,
考虑下,其实是数学的组合问题,简单演示在草稿上,如果两个DropDownList,则只有四种可能,要么是"全部"要么不是"全部"所以有以下结果
string sql;
sql = "select * from file where 1=1";//一开始要赋给它,否则,编译时候不通过
if(DropDownList3 .SelectedValue =="全部" && DropDownList4 .SelectedValue =="全部")
sql = "select * from file where date_year='" + DropDownList1.SelectedValue + "'and subject ='" + DropDownList2.SelectedValue + "'";
if (DropDownList3.SelectedValue == "全部" && DropDownList4.SelectedValue != "全部")
sql = "select * from file where date_year='" + DropDownList1.SelectedValue + "'and subject ='" + DropDownList2.SelectedValue + "'and grade ='" + DropDownList4.SelectedValue + "'";
if (DropDownList3.SelectedValue != "全部" && DropDownList4.SelectedValue == "全部")
sql = "select * from file where date_year='" + DropDownList1.SelectedValue + "'and subject ='" + DropDownList2.SelectedValue + "'and person_shenfen ='" + DropDownList3.SelectedValue + "'";
if (DropDownList3.SelectedValue != "全部" && DropDownList4.SelectedValue != "全部")
sql = "select * from file where date_year='" + DropDownList1.SelectedValue + "'and subject ='" + DropDownList2.SelectedValue + "'and person_shenfen ='" + DropDownList3.SelectedValue + "'and grade ='" + DropDownList4.SelectedValue + "'";
这样就实现了sql语句的查询了.
如果有什么好的建议和意见,欢迎批评指正,谢谢.