zoukankan      html  css  js  c++  java
  • 多条件搜索分页的实现

      对教材列表进行展示的时候,可以有条件的筛选结果,前台用到的是EasyUI DataGrid,后台接受查询参数,返回结果。

         这里只写成后台,数据访问层中实现的关键代码。

    View Code
      1  /// <summary>
      2         /// 默认下获取分页数据
      3         /// </summary>
      4         /// <param name="pageIndex">当前页码</param>
      5         /// <param name="pageSize">每一页的行数</param>
      6         /// <returns>BookID,BookName,Author,UserName,AcaName,Category,[State],AddDate,LastUpdateTime</returns>
      7         public DataTable GetPagedModel(int pageIndex, int pageSize) 
      8         {
      9             string strOrder = "order by AddDate DESC";
     10             string strSQL = string.Format(@"select * from
     11                 (select BookID,BookName,Author,UserName,AcaName,Category,[State],AddDate,LastUpdateTime,ROW_NUMBER() over({0}) as num from ViewBookInfo where IsDel=0) as tb 
     12                 where num between {1}*({2}-1)+1 and {1}*{2} {0} ", strOrder, pageSize, pageIndex);
     13 
     14             return  SQLHelper.GetDataTable(strSQL);
     15         }
     16 
     17         /// <summary>
     18         /// 含参数的查询
     19         /// </summary>
     20         /// <param name="pageIndex"></param>
     21         /// <param name="pageSize"></param>
     22         /// <param name="paras">BookName、ClassName、DeptID、DeptPID、Category、[State]</param>
     23         /// <returns>BookID,BookName,Author,UserName,AcaName,Category,[State],AddDate,LastUpdateTime</returns>
     24         public DataTable GetPagedModel(int pageIndex, int pageSize, Hashtable ht) 
     25         {
     26             string strCondition;
     27             List<SqlParameter> parasList;
     28             string sOrder=" order by AddDate DESC";
     29 
     30             //获取参数
     31             GetSearchCondition(ht,out strCondition,out parasList);
     32 
     33             string strSQL = string.Format(@"select * from (select BookID,BookName,Author,UserName,AcaName,Category,[State],AddDate,LastUpdateTime,ROW_NUMBER() over({0}) as num from ViewBookInfo where IsDel=0 {3}) as tb 
     34                 where num between {1}*({2}-1)+1 and {1}*{2} {0} ", sOrder , pageSize, pageIndex,strCondition);
     35 
     36             return SQLHelper.GetDataTable(strSQL,parasList.ToArray());
     37 
     38         }
     39 
     40         /// <summary>
     41         /// 获取详细信息
     42         /// </summary>
     43         /// <param name="BookID"></param>
     44         /// <returns></returns>
     45         public DataTable GetDetail(int BookID) 
     46         {
     47             string strSQL = "select ClassTarget,BookFeature from BookInfo where BookID="+BookID;
     48 
     49             return SQLHelper.GetDataTable(strSQL);
     50         }
     51         /// <summary>
     52         /// 含参数记录的条数
     53         /// </summary>
     54         /// <param name="ht">BookName、ClassName、DeptID、DeptPID、Category、[State]</param>
     55         /// <returns></returns>
     56         public int GetRecordCount(Hashtable ht) 
     57         {
     58             string strCondition;
     59             List<SqlParameter> parasList;
     60 
     61             //获取参数
     62             GetSearchCondition(ht, out strCondition, out parasList);
     63 
     64             string strSQL = string.Format("select count(0) from ViewBookInfo where IsDel=0 {0} ", strCondition);
     65 
     66             return Convert.ToInt32(SQLHelper.ExecuteScalar(strSQL, parasList.ToArray()));
     67         }
     68 
     69         /// <summary>
     70         /// 装配查询参数,获取查询要的字符串,和参数集合
     71         /// </summary>
     72         /// <param name="ht">BookName、ClassName、DeptID、DeptPID、Category、[State]</param>
     73         /// <param name="sOrder"></param>
     74         /// <param name="paras"></param>
     75         private void GetSearchCondition(Hashtable ht, out string contString, out List<SqlParameter> paras) 
     76         {
     77             StringBuilder sOrder = new StringBuilder();
     78             paras=new List<SqlParameter>();
     79 
     80             if (ht.Count == 0) 
     81             {
     82                 contString = "";
     83                 return;
     84             }
     85 
     86             object oBookName = ht["BookName"];
     87             object oClassName = ht["ClassName"];
     88             object oDeptID = ht["DeptID"];
     89             object oDeptPID = ht["DeptPID"];
     90             object oCategory = ht["Category"];
     91             object oState = ht["State"];
     92 
     93             //书名
     94             if (oBookName != null && !string.IsNullOrEmpty(oBookName.ToString()))
     95             {
     96                 sOrder.Append(" and BookName like @BookName or  BookPY like @BookName");
     97                 paras.Add(new SqlParameter("@BookName",oBookName.ToString()+"%"));
     98             }
     99             //课程名
    100             if(oClassName!=null&&!string.IsNullOrEmpty(oClassName.ToString()))
    101             {
    102                 sOrder.Append(" and  ClassName like @ClassName ");
    103                 paras.Add(new SqlParameter("@ClassName",oClassName.ToString()+"%"));
    104             }
    105             //部门ID
    106             if (oDeptID != null && !string.IsNullOrEmpty(oDeptID.ToString())) 
    107             {
    108                 sOrder.Append(" and DeptID=@DeptID ");
    109                 paras.Add(new SqlParameter("@DeptID",Convert.ToInt32(oDeptID)));
    110             }
    111 
    112             //上级部门PID
    113             if (oDeptPID != null && !string.IsNullOrEmpty(oDeptPID.ToString())) 
    114             {
    115                 sOrder.Append(" and DeptPID=@DeptPID ");
    116                 paras.Add(new SqlParameter("@DeptPID",Convert.ToInt32(oDeptPID)));
    117             }
    118 
    119             //种类Category
    120             if (oCategory != null && !string.IsNullOrEmpty(oCategory.ToString())) 
    121             {
    122                 sOrder.Append(" and Category=@Category ");
    123                 paras.Add(new SqlParameter("@Category",oCategory.ToString()));
    124             }
    125 
    126             //状态State
    127             if (oState != null && !string.IsNullOrEmpty(oState.ToString())) 
    128             {
    129                 sOrder.Append(" and [State]=@State ");
    130                 paras.Add(new SqlParameter("@State",oState.ToString()));
    131             }
    132 
    133             contString = sOrder.ToString();
    134             
    135         }

      条件查询中Hashtable存储的是筛选的字段和其对应的值。

  • 相关阅读:
    Android 获取Bitmap方式
    o.a.catalina.core.AprLifecycleListener : An incompatible version [1.2.7] of the APR based Apache Tomcat Native library is installed, while Tomcat requires version [1.2.14]
    idea 全部报错找不到包
    idea Plugin "Maven Integration Extension" was not loaded: required plugin "Maven Integration" is disabled
    okhttp 通过网关请求服务端返回数据
    mongo 过滤查询条件后分组、排序
    OKhttp3
    linux安装consul集群
    spring cloud 订单调用用户
    WebSphere概要文件管理工具
  • 原文地址:https://www.cnblogs.com/dedeyi/p/2957790.html
Copyright © 2011-2022 走看看