zoukankan      html  css  js  c++  java
  • 关于条件查询的参数condition的最佳方案

            当我们在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 累加的方式,这里略!

  • 相关阅读:
    MySQL5.7二进制安装及多实例
    MySQL5.7版本的yum安装方式
    PHP安装
    MySQL5.6安装部署及多实例主从
    Prometheus监控MySQL和Linux主机结合Grafana出图
    MySQL5.7源码安装(编译)
    MySQL基本管理
    WC框架
    .NET 调用虚方法2 转
    .NET 调用虚方法1 转
  • 原文地址:https://www.cnblogs.com/yingger/p/2601316.html
Copyright © 2011-2022 走看看