nhibernate 分页

 /// <summary>
        /// 分页
        /// </summary>
        /// <param name="pageNumber">页码</param>
        /// <param name="pageSize">每页显示行数</param>
        /// <param name="NiceName">昵称</param>
        /// <param name="phone">电话</param>
        /// <returns></returns>
        public List<UserInfo> Search(int pageNumber, int pageSize, string NiceName, string phone)
        {
            ISession session = null;
            try
            {
                List<string> idList = new List<string>();
                idList = (List<string>)SearchIdList();
                session = SessionFactory.OpenSession();
                ICriteria criteria = session.CreateCriteria(typeof(UserInfo));
                criteria.Add(Restrictions.In("Id", idList));
                if (NiceName != "")
                {
                    criteria.Add(Restrictions.Like("NickName", "%" + NiceName + "%"));
                }
                if (phone != "")
                {
                    criteria.Add(Restrictions.Like("Mobile", "%" + phone + "%"));
                }
                criteria.AddOrder(Order.Desc("InputDate"));
                criteria.SetFirstResult(pageSize * pageNumber - pageSize);//开始位置
                criteria.SetMaxResults(pageSize);//每页数据大小
                IList<UserInfo> result = criteria.List<UserInfo>();
                return result.ToList();
            }
            catch (Exception ex)
            {
                //记录日志
                SqlAccessLogger.Write("UserInfoDAL.Search(List<string> idList)", "UserInfoDAL.Search() ", ex);
                //抛出异常
                throw;
            }
            finally
            {
                if (session != null && session.IsOpen)
                {
                    session.Close();
                }
            }
        }

 

  /// <summary>
        /// 分页总数据行数
        /// </summary>
        /// <param name="pageNumber">页码</param>
        /// <param name="pageSize">每页显示行数</param>
        /// <param name="NiceName">昵称</param>
        /// <param name="phone">电话</param>
        /// <returns></returns>
        public int GetTotalCount(int pageNumber, int pageSize, string key, string phone)
        {
            ISession session = null;
            try
            {
                List<string> idList = new List<string>();
                idList = (List<string>)SearchIdList();
                session = SessionFactory.OpenSession();
                ICriteria criteria = session.CreateCriteria(typeof(UserInfo));
                criteria.Add(Restrictions.In("Id", idList));
                if (key != "")
                {
                    criteria.Add(Restrictions.Like("NickName", "%" + key + "%"));
                }
                if (phone != "")
                {
                    criteria.Add(Restrictions.Like("Mobile", "%" + phone + "%"));
                }
                IList<UserInfo> result = criteria.List<UserInfo>();
                return result.Count;
            }
            catch (Exception ex)
            {
                //记录日志
                SqlAccessLogger.Write("UserInfoDAL.Search(List<string> idList)", "UserInfoDAL.Search() ", ex);
                //抛出异常
                throw;
            }
            finally
            {
                if (session != null && session.IsOpen)
                {
                    session.Close();
                }
            }
        }

基于这两个方法就可以实现数据分页了。

ICriteria 的简单学习地址:http://blog.knowsky.com/213234.htm

posted @ 2013-11-25 14:52  闪存第一莽撞人  阅读(161)  评论(0编辑  收藏  举报