码迷,mamicode.com
首页 > 数据库 > 详细

分页SQL

时间:2014-06-28 16:54:16      阅读:282      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   get   数据   os   

一、Oracle

1、SQL

-- order by
SELECT *
  FROM (SELECT ORACLEPAGESQL1.*, ROWNUM RN
          FROM (SELECT *
                  FROM (SELECT IDX, NAME, CH_NAME FROM TBL_USER) ORACLEPAGESQL
                 ORDER BY IDX ASC) ORACLEPAGESQL1
         WHERE ROWNUM <= 20) ORACLEPAGESQL2
 WHERE RN > 10;

-- no order by 
SELECT *
  FROM (SELECT ORACLEPAGESQL1.*, ROWNUM RN
          FROM (SELECT IDX, NAME, CH_NAME FROM TBL_USER) ORACLEPAGESQL1
         WHERE ROWNUM <= 20) ORACLEPAGESQL2
 WHERE RN > 10;

2、CODE

 /// <summary>
        /// 分页获取当前页记录
        /// </summary>
        /// <param name="PageIndex">当前页面</param>
        /// <param name="PageSize">每页记录数</param>
        /// <param name="strSQL">查询SQL</param>
        /// <param name="strOrderBy">分组</param>
        /// <returns>分页SQL</returns>
        public string GetOraclePageSQL(int PageIndex, int PageSize, string strSQL, string strOrderBy)
        {
            string result = "";
            int startIndex = (PageIndex - 1) * PageSize;
            int endIndex = startIndex + PageSize;
            if (!string.IsNullOrEmpty(strOrderBy))
            {
                result = "SELECT * FROM (" + strSQL + ") oraclePageSQL ORDER BY " + strOrderBy;
            }
            else
            {
                result = strSQL;
            }
            result = "SELECT * FROM (SELECT oraclePageSQL1.*,rownum rn FROM (" + result + ") oraclePageSQL1 WHERE rownum<=" + endIndex + ") oraclePageSQL2 WHERE rn>" + startIndex;

            return result;
        }

        /// <summary>
        /// 分页获取总记录数
        /// </summary>
        /// <param name="strSQL">查询SQL</param>
        /// <returns>总记录数SQL</returns>
        public string GetOraclePageCoutSQL(string strSQL)
        {
            string result = "";
            result = "SELECT COUNT(*) totalNum FROM (" + strSQL + ") TOTALNUM  ";

            return result;
        }

 

二、MySQL

1、SQL

SELECT * FROM zt_history limit 0,10

2、CODE

        /// <summary>
        /// 获取数据集以及分页总记录数
        /// </summary>
        /// <param name="PageIndex">当前页面</param>
        /// <param name="PageSize">每页记录数</param>
        /// <param name="strSQL">SQL</param>
        /// <param name="count">总记录数</param>
        /// <returns>数据集</returns>
        private DataTable GetMySQLDt(int PageIndex, int PageSize,string strSQL,out int count)
        {
            try
            {
                string strCon = "Data Source=localhost;Database=zentao;User Id=root;Password=;port=3309";
                int intStart = (PageIndex -1) * PageSize;
                string strPageSQL = strSQL + " limit " + intStart + "," + PageSize;
                DataSet ds = MySqlHelper.ExecuteDataset(strCon, strPageSQL);

                string strCountSql = "SELECT COUNT(*) totalNum FROM (" + strSQL + ") TOTALNUM ";
                DataSet dsCount = MySqlHelper.ExecuteDataset(strCon, strCountSql);
                count = Convert.ToInt32(dsCount.Tables[0].Rows[0][0]);

                return ds.Tables[0];
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

 

分页SQL,布布扣,bubuko.com

分页SQL

标签:style   blog   color   get   数据   os   

原文地址:http://www.cnblogs.com/illday/p/3797653.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!