日期:2014-05-18  浏览次数:20996 次

如何对窗口中的绑定字段进行多条件识别,生成SQL查询语句?
在一个员工信息档案里,有职务级别、学历层次、政治面貌等,我在界面上有这三个字段的查询条件输入控件(TextBox),请问如何进行综合查询。
如我只输入了职务级别,没有输入学历层次与政治面貌,那怎样生成查询语句呢?我想用一个if语句将CheckBox判断对应的是否将字段作为查询条件,然后写不同的查询语句,但这样我觉得不科学,因为虽着要查询的字段 能不能动态生成呢?就与函数中的默认参数一样。
先说句谢谢了!

------解决方案--------------------
C# code

string strCmd="select * from tablename where 1=1";
if (!string.IsNullOrEmpty(职务级别.Text))
{
    strCmd += " and 职务级别='"+职务级别.Text+"'";
}
if (!string.IsNullOrEmpty(职务级别.Text))
{
    strCmd += " and 学历层次='"+学历层次.Text+"'";
}
if (!string.IsNullOrEmpty(职务级别.Text))
{
    strCmd += " and 政治面貌='"+政治面貌.Text+"'";
}

------解决方案--------------------
数据量不大,可以用DataView过滤
------解决方案--------------------
探讨
C# code
string strCmd="select * from tablename where 1=1";
if (!string.IsNullOrEmpty(职务级别.Text))
{
strCmd += " and 职务级别='"+职务级别.Text+"'";
}
if (!string.IsNullOrEmpty(职务级别.Text))
{
strCmd += " and 学历层次='"+学历层次.Text+"'";
}
if (!string.IsNullOrEmpty(职务级别.Text))
{
strCmd += " and 政治面貌='"+政治面貌.Text+"'";
}