Exemplo n.º 1
0
        /// <summary>
        /// 设置分页结果
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="pageResult"></param>
        /// <returns></returns>
        public virtual PageResultEntity SetPageListResult <T>(PageResultEntity pageResult)
        {
            if (pageResult.Data == null)
            {
                return(pageResult);
            }
            var dataList = pageResult.Data as List <T>;

            if (dataList.Count <= 0)
            {
                pageResult.TotalPageIndex = 0;
                return(pageResult);
            }
            else if (pageResult.TotalCount > 0 && pageResult.TotalCount <= pageResult.PageSize)
            {
                pageResult.TotalPageIndex = 1;
                return(pageResult);
            }
            else
            {
                if (pageResult.TotalCount % pageResult.PageSize == 0)
                {
                    pageResult.TotalPageIndex = pageResult.TotalCount / pageResult.PageSize;
                }
                else
                {
                    pageResult.TotalPageIndex = pageResult.TotalCount / pageResult.PageSize + 1;
                }
            }
            return(pageResult);
        }
Exemplo n.º 2
0
        /// <summary>
        /// 单表分页数据
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="queryColumns">要查询字段</param>
        /// <param name="sortColumn">排序字段</param>
        /// <param name="sortType">排序方式</param>
        /// <param name="pageSize">每页条数</param>
        /// <param name="pageIndex">第几页</param>
        /// <param name="whereSql">过滤条件Sql</param>
        /// <param name="whereParam">过滤条件字段名及字段值参数,例:new {Uname="joyet",Age = 110}</param>
        /// <returns></returns>
        public virtual PageResultEntity QueryPageList <T>(string queryColumns, string sortColumn, string sortType, int pageSize, int pageIndex, string whereSql, object whereParam)
        {
            PageResultEntity result = new PageResultEntity();

            if (pageSize <= 0 || pageIndex <= 0)
            {
                return(result);
            }
            result.PageSize         = pageSize;
            result.CurrentPageIndex = pageIndex;

            #region 为了分页查询效率,查询第一页时才会查询所有条数
            if (result.CurrentPageIndex == 1)
            {
                var totalPageDbEntity = DbBuilder.QueryTotalPageCount <T>(whereSql, whereParam);
                if (totalPageDbEntity == null)
                {
                    return(result);
                }
                var objTotalCount = DbHelper.ExecuteScalar(totalPageDbEntity.CommandText, totalPageDbEntity.DbParams);
                if (objTotalCount == null)
                {
                    return(result);
                }
                result.TotalCount = Convert.ToInt64(objTotalCount);
                if (result.TotalCount <= 0)
                {
                    return(result);
                }
            }
            #endregion

            var dbEntity = DbBuilder.QueryPageList <T>(queryColumns, sortColumn, sortType, pageSize, pageIndex, whereSql, whereParam);
            if (dbEntity == null)
            {
                return(result);
            }
            using (var reader = DbHelper.ExecuteReader(dbEntity.CommandText, dbEntity.DbParams))
            {
                var datas = DataReaderToEntityList <T>(reader);
                result.Data = datas;
                result      = SetPageListResult <T>(result);
            }
            HandelExecuteDbCallBack(dbEntity.CommandText, dbEntity.DbParams);
            return(result);
        }