Пример #1
0
        /// <summary>
        /// 执行分页数据查询操作,返回PageResponseData
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="connectionStr"></param>
        /// <param name="request"></param>
        /// <returns></returns>
        public override PageResponseData GetPageList <T>(string connectionStr, PageRequestData pageRequest)
        {
            PageResponseData responsResult = new PageResponseData();

            try
            {
                #region 相关变量定义
                Type   type         = typeof(T);
                string tableName    = type.Name;
                int    startNum     = pageRequest.PageSize * (pageRequest.PageIndex - 1) + 1;
                int    endNum       = pageRequest.PageSize * pageRequest.PageIndex;
                var    searchEntity = pageRequest.SearchEntityObj;
                #endregion

                #region 分页查询处理
                EntityColumn idColumn = new EntityColumnUtility().GetIdColumn <T>(null);
                if (idColumn != null)
                {
                    responsResult.TotalCount = GetCount <T>(connectionStr, searchEntity);
                    if (responsResult.TotalCount > 0)
                    {
                        if (string.IsNullOrEmpty(searchEntity.ColumnSql))
                        {
                            searchEntity.ColumnSql = "b.*";
                        }
                        if (string.IsNullOrEmpty(searchEntity.SortColumn))
                        {
                            searchEntity.SortColumn = idColumn.ColumnName;
                            searchEntity.SortMethod = "asc";
                        }
                        StringBuilder cmdText = new StringBuilder();
                        cmdText.AppendFormat("select {0}  from ", searchEntity.ColumnSql);
                        cmdText.Append("(");
                        cmdText.AppendFormat("select ROW_NUMBER() over(order by {0} {1}) num,{2}  from {3} ", searchEntity.SortColumn, searchEntity.SortMethod, idColumn.ColumnName, tableName);
                        if (!string.IsNullOrEmpty(searchEntity.WhereSql))
                        {
                            cmdText.AppendFormat("where {0} ", searchEntity.WhereSql);
                        }
                        cmdText.Append(") ");
                        cmdText.AppendFormat("a inner join {0} b on a.{1}=b.{1} and  a.num between {2} and {3} ", tableName, idColumn.ColumnName, startNum, endNum);
                        IDataReader dataReader;
                        if (searchEntity.WhereParam != null)
                        {
                            dataReader = SqlDbBase.ExecuteReaderWithParam(connectionStr, cmdText.ToString(), CommandType.Text, searchEntity.WhereParam);
                        }
                        else
                        {
                            dataReader = SqlDbBase.ExecuteReader(connectionStr, cmdText.ToString(), CommandType.Text, null);
                        }
                        responsResult.Data = Map <T>(dataReader);
                    }
                }
                #endregion
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return(responsResult);
        }
Пример #2
0
        /// <summary>
        /// 执行分页数据查询操作,返回PageResponseData
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="connectionStr"></param>
        /// <param name="request"></param>
        /// <returns></returns>
        public override PageResponseData GetPageList <T>(string connectionStr, PageRequestData pageRequest)
        {
            PageResponseData responsResult = new PageResponseData();

            try
            {
                #region 相关变量定义
                Type   type         = typeof(T);
                string tableName    = type.Name;
                int    startNum     = pageRequest.PageSize * (pageRequest.PageIndex - 1);
                var    searchEntity = pageRequest.SearchEntityObj;
                #endregion

                #region 分页查询处理
                EntityColumn idColumn = new EntityColumnUtility().GetIdColumn <T>(null);
                if (idColumn != null)
                {
                    //查询记录结果数
                    responsResult.TotalCount = GetCount <T>(connectionStr, searchEntity);
                    if (responsResult.TotalCount > 0)
                    {
                        StringBuilder cmdText = new StringBuilder();
                        if (string.IsNullOrEmpty(searchEntity.ColumnSql))
                        {
                            searchEntity.ColumnSql = "*";
                        }
                        if (string.IsNullOrEmpty(searchEntity.SortColumn))
                        {
                            searchEntity.SortColumn = idColumn.ColumnName;
                        }
                        cmdText.AppendFormat("select {0} from {1} ", searchEntity.ColumnSql, tableName);
                        if (string.IsNullOrEmpty(searchEntity.WhereSql))
                        {
                            cmdText.AppendFormat("where {0} ", searchEntity.WhereSql);
                        }
                        cmdText.AppendFormat("order by {0} {1} ", searchEntity.SortColumn, searchEntity.SortMethod);
                        cmdText.AppendFormat("limit {0},{1} ", startNum, pageRequest.PageSize);
                        IDataReader dataReader;
                        if (searchEntity.WhereParam != null)
                        {
                            dataReader = SqlDbBase.ExecuteReaderWithParam(connectionStr, cmdText.ToString(), CommandType.Text, searchEntity.WhereParam);
                        }
                        else
                        {
                            dataReader = SqlDbBase.ExecuteReader(connectionStr, cmdText.ToString(), CommandType.Text, null);
                        }
                        responsResult.Data = Map <T>(dataReader);
                    }
                }
                #endregion
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return(responsResult);
        }