获取分页的查询语句-针对MS2005和Oracle

来源:互联网 发布:ajaxupload.js官网 编辑:程序博客网 时间:2024/06/10 21:17

        /// <summary>
        /// 返回分页SQL语句
        /// </summary>
        /// <param name="selectSql">查询SQL语句</param>
        /// <param name="PageIndex">当前页码</param>
        /// <param name="PageSize">一页多少条记录</param>
        /// <returns></returns>
        public static string getPageSplitSQL(string selectSql, int PageIndex, int PageSize)
        {
            string StartSelectSql = @" select * from (select aa.*, rownum r  from (";
            int CurrentReadRows = PageIndex * PageSize;
            int startRow = (CurrentReadRows - PageSize) < 0 ? 0 : (CurrentReadRows - PageSize);
            int endRow = CurrentReadRows == 0 ? PageSize : CurrentReadRows;
            string EndSelectSql = string.Format(") aa where rownum <= {1}) bb  where r >{0} ", startRow, endRow);
            return StartSelectSql + selectSql + EndSelectSql;
        }

原创粉丝点击