当我们在DAL层写一个方法进行对数据的条件查询时:
public IList<UserInfo> GetUsersByCondition( stiring condition )
{
string strSql=string.Format(" select * from UserInfo where {0}" ,conditon);
return GetUsreInfoBySql( strSql );
}
时须在界面层对condition有二种处理:
1.使用System.Text.StringBuilder处理,这种处理方式对于条件很多的情况有助于代码效率和内存使用。下面是查询按钮下的代码:
protected void btnSearch_Click( object sender,EventArgs e )
{
System.Text.StringBuilder sb=new System.Text.StringBuilder();
sb.Append(" 1=1 ");
if ( ddlBranchThree.SelectedValue != "-1")
{
sb.AppendFormat(" and Ctk_no={0}", ddlBranchThree.SelectedValue);
}
if ( ddlBranchTwo.SelectedValue != "-1")
{
sb.AppendFormat( " and Cn_no={0}",ddlBranchTwo.SelectedValue);
}
if ( TxtTimeFirst.Value!=""&& TxtTimeEnd.Value!="")
{
sb.AppandFormat(" and Create_Time between '{0}' and '{1}' ",TxtTimeFirst.Value+"0:00:00",TxtTimeEnd.Value+"23:59:59");
}
string condition = sb.ToString();
Session["condition"] = condition ; //传向另一个页 面
......
}
第二种 以string 累加的方式,这里略!