C# 分页方法

    /// <summary>
    /// 分页实体
    /// </summary>
    public class PageInfo
    {
        private int iPage;
        /// <summary>
        /// 当前页码
        /// </summary>
        public int IPage
        {
            get { return iPage; }
            set { iPage = value; }
        }
        private int iPageSize;
        /// <summary>
        /// 每页条数
        /// </summary>
        public int IPageSize
        {
            get { return iPageSize; }
            set { iPageSize = value; }
        }
        private string strTable;
        /// <summary>
        /// 查询的表
        /// </summary>
        public string StrTable
        {
            get { return strTable; }
            set { strTable = value; }
        }
        private string strText;
        /// <summary>
        /// 查询的字段
        /// </summary>
        public string StrText
        {
            get { return strText; }
            set { strText = value; }
        }
        private string strWhere;
        /// <summary>
        /// 查询的条件
        /// </summary>
        public string StrWhere
        {
            get { return strWhere; }
            set { strWhere = value; }
        }
        private string strIndex;
        /// <summary>
        /// 索引
        /// </summary>
        public string StrIndex
        {
            get { return strIndex; }
            set { strIndex = value; }
        }
        private string strOrder;
        /// <summary>
        /// 排序字段
        /// </summary>
        public string StrOrder
        {
            get { return strOrder; }
            set { strOrder = value; }
        }
    }

SqlServer

        /// <summary>
        /// 分页方法
        /// </summary>
        /// <param name="p">分页实体</param>
        /// <param name="parameters">查询参数</param>
        /// <param name="outCount">数据总数</param>
        /// <returns></returns>
        public static DataTable Page(PageInfo p, SqlParameter[] parameters, out int outCount)
        {
            string SqlCount, Sql, TempOrder;
            if (p.StrOrder != "")
            { TempOrder = " order by " + p.StrOrder; }
            else
            { TempOrder = ""; }
            if (p.IPage == 1)
            {
                Sql = "select top " + p.IPageSize + " " + p.StrText + " from " + p.StrTable + " where 1=1 " + p.StrWhere +
                  TempOrder;
            }
            else
            {
                Sql = "select top " + p.IPageSize + " " + p.StrText + " from " + p.StrTable + " where " + p.StrIndex +
                  " not in (select top " + (p.IPageSize * (p.IPage - 1)) + " " + p.StrIndex + " from " + p.StrTable +
                  " where 1=1 " + p.StrWhere + TempOrder + ") " + p.StrWhere + TempOrder;
            }
            SqlCount = "select isnull(count(*),10000)   from " + p.StrTable + "  where 1=1 " + p.StrWhere;
            string ls_Temp = SqlHelper.ExecuteScalar(SqlCount, parameters);
            try
            { outCount = Convert.ToInt32(ls_Temp); }
            catch { outCount = 0; }
            DataTable dt = SqlHelper.ExecuteDataTable(CommandType.Text, Sql, parameters);
            return dt;
        }

MySQL

        /// <summary>
        /// 分页方法
        /// </summary>
        /// <param name="p">分页实体</param>
        /// <param name="parameters">查询参数</param>
        /// <param name="outCount">数据总数</param>
        /// <returns></returns>
        public static DataTable Page(PageInfo p, SqlParameter[] parameters, out int outCount)
        {
            string SqlCount, Sql, TempOrder;
            if (p.StrOrder != "")
            { TempOrder = " order by " + p.StrOrder; }
            else
            { TempOrder = ""; }
            if (p.IPage == 1)
            {
                Sql = "select " + p.StrText + " from " + p.StrTable + " where 1=1 " + p.StrWhere +
                  TempOrder + " limit 0," + p.IPageSize;
            }
            else
            {
                Sql = "select " + p.StrText + " from " + p.StrTable + " where 1=1 " + p.StrWhere +
                  TempOrder + " limit " + (p.IPageSize * (p.IPage - 1)) + "," + p.IPageSize;
            }
            SqlCount = "select ifnull(count(*),10000)   from " + p.StrTable + "  where 1=1 " + p.StrWhere;
            string ls_Temp = SqlHelper.ExecuteScalar(SqlCount, parameters);
            try
            { outCount = Convert.ToInt32(ls_Temp); }
            catch { outCount = 0; }
            DataTable dt = SqlHelper.ExecuteDataTable(CommandType.Text, Sql, parameters);
            return dt;
        }

 

转载于:https://www.cnblogs.com/bo10296/p/5139451.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值