.net access分页类代码

1,977次阅读
没有评论
public class PageData
    {
        private int _PageSize = 10;
        private int _PageIndex = 1;
        private int _PageCount = 0;
        private int _TotalCount = 0;
        private string _TableName;//表名
        private string _QueryFieldName = "*";//表字段FieldStr
        private string _OrderStr = string.Empty; //排序_SortStr
        private string _QueryCondition = string.Empty;//查询的条件 RowFilter
        private string _PrimaryKey = string.Empty;//主键
        private bool _isQueryTotalCounts = true;//是否查询总的记录条数
        /// 
        /// 是否查询总的记录条数
        /// 
        public bool IsQueryTotalCounts
        {
            get { return _isQueryTotalCounts; }
            set { _isQueryTotalCounts = value; }
        }
        /// 
        /// 显示页数
        /// 
        public int PageSize
        {
            get
            {
                return _PageSize;

            }
            set
            {
                _PageSize = value;
            }
        }
        /// 
        /// 当前页
        /// 
        public int PageIndex
        {
            get
            {
                return _PageIndex;
            }
            set
            {
                _PageIndex = value;
            }
        }
        /// 
        /// 总页数
        /// 
        public int PageCount
        {
            get
            {
                return _PageCount;
            }
        }
        /// 
        /// 总记录数
        /// 
        public int TotalCount
        {
            get
            {
                return _TotalCount;
            }
        }
        /// 
        /// 表名,包括视图
        /// 
        public string TableName
        {
            get
            {
                return _TableName;
            }
            set
            {
                _TableName = value;
            }
        }
        /// 
        /// 表字段FieldStr
        /// 
        public string QueryFieldName
        {
            get
            {
                return _QueryFieldName;
            }
            set
            {
                _QueryFieldName = value;
            }
        }
        /// 
        /// 排序字段
        /// 
        public string OrderStr
        {
            get
            {
                return _OrderStr;
            }
            set
            {
                _OrderStr = value;
            }
        }
        /// 
        /// 查询条件
        /// 
        public string QueryCondition
        {
            get
            {
                return _QueryCondition;
            }
            set
            {
                _QueryCondition = value;
            }
        }
        /// 
        /// 主键
        /// 
        public string PrimaryKey
        {
            get {
                return _PrimaryKey;
            }
            set {
                _PrimaryKey = value;
            }
        }
        public DataSet QueryDataTable()
        {
            if (_isQueryTotalCounts)
            {
                _TotalCount = GetTotalCount();
            }
            if (_TotalCount == 0)
            {
                _PageIndex = 0;
                _PageCount = 0;
            }
            else
            {
                _PageCount = _TotalCount % _PageSize == 0 ? _TotalCount / _PageSize : _TotalCount / _PageSize + 1;
                if (_PageIndex > _PageCount)
                {
                    _PageIndex = _PageCount;

                }
            }
            string strsql = "SELECT TOP "+this.PageSize+" "+this.QueryFieldName+" from "+this.TableName +" where "+this.PrimaryKey
                +" not in(select top "+this.PageIndex*this.PageSize+" from "+this.TableName+" where 1=1 "+this.QueryCondition+" order by "+this.OrderStr+" desc) "+this.QueryCondition+" order by "
                +this.OrderStr+" desc";


            return AccessDb.DQuery(strsql);
        }

        public int GetTotalCount()
        {
            string strSql = " select count("+this.PrimaryKey+") from "+_TableName;
            if (_QueryCondition != string.Empty)
            {
                strSql +=" where 1=1" + _QueryCondition;
            }
            return (int)AccessDb.ExecuteScalar(strSql);
        }
    }
正文完
 

公众号