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

  • 相关阅读:
    奔溃瞬间1
    面试知识点blog汇总
    贪心
    树 和 图
    DFS 和 BFS
    STL
    哈希表
    手写堆
    并查集
    二项式反演学习笔记
  • 原文地址:https://www.cnblogs.com/yingger/p/2601316.html
Copyright © 2011-2022 走看看