把一般的查询sql处理成分页用的sql

        //// <summary>
        /// 把一般的查询sql处理成分页用的sql  用于sql2005以上
        /// </summary>
        /// <param name="sql">一般的sql</param>
        /// <param name="pn">当前页数</param>
        /// <param name="countPerPage">每一页的个数</param>
        /// <returns>返回分页用的sql</returns>
        public static string SQLFenyeChuli(string sql, int pn, int countPerPage)
        {
            try
            {
                string topCount = (pn * countPerPage).ToString();//获取获取前多少条数据
                string strAlreadyCount = ((pn - 1) * countPerPage).ToString();//获取已经查询的数据

                string rtn = sql.Insert(6, " top " + topCount + " 0 as tempcolumn,");//在select后面插入分页用的数据
                rtn = "select * from (select row_number() over (order by tempcolumn) temprow, * from (" + rtn + ")tt)t where temprow >" + strAlreadyCount;//row_number() 用于sql2005以上

                return rtn;
            }
            catch
            {
                return "";
            }
        }

posted @ 2010-12-07 20:16  sweetjian  阅读(243)  评论(0编辑  收藏  举报