Exemplo n.º 1
0
        /// <summary>
        /// 分页导出数据,PageIndex从1开始
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="connInfo"></param>
        /// <param name="sql"></param>
        /// <param name="Pars"></param>
        /// <param name="PageSize">为零不限制页面大小即返回所有数据</param>
        /// <param name="PageIndex">PageIndex从1开始</param>
        /// <param name="returnTotalRows"></param>
        /// <param name="TotalRows"></param>
        /// <returns></returns>
        public static DataTable SelectPageDataTable(this DbContext connInfo,
                                                    string sql, RequestBase param, int PageSize, int PageIndex, bool returnTotalRows, ref int TotalRows,
                                                    string filter = "", string orderStatement = "", Dictionary <string, object> condition = null)
        {
            bool getAll = (0 == PageSize);

            if (PageSize < 0)
            {
                throw new Exception("PageSize must greater or equal than 0!");
            }
            if (!getAll && PageIndex < 1)
            {
                throw new Exception("PageIndex must greater or equal than 1!");
            }
            StringBuilder sqlbld = new StringBuilder(1024);

            sqlbld.Append(sql);
            bool hasCondi = (!string.IsNullOrEmpty(filter) || (null != condition && 0 < condition.Count)); //无过滤

            if (hasCondi)
            {
                bool containWhere = 0 <= sql.IndexOf("WHERE ", StringComparison.OrdinalIgnoreCase);
                sqlbld.Append(containWhere ? " AND " : " WHERE ");
                sqlbld.Append(filter);
                AddParameters(condition, ref param);
            }
            AppendOrderBy(orderStatement, sqlbld);
            string SQL = sqlbld.ToString();

            if (!getAll)
            {
                if (returnTotalRows)
                {
                    TotalRows = 0;
                    TotalRows = Count(connInfo, SQL, param);
                }
                return(connInfo.GetDataTablePage(SQL, (PageIndex - 1) * PageSize, PageSize, param));
            }
            else
            {
                DataTable result = connInfo.GetDataTable(SQL, param);
                if (returnTotalRows)
                {
                    TotalRows = (null != result ? result.Rows.Count : 0);
                }
                return(result);
            }
        }
Exemplo n.º 2
0
 public DataTable GetDataTable()
 {
     return(_connInfo.GetDataTable(GetSql(), param, cmdType: _CmdType));
 }