分页思路1

1.先查询出已经看过的数据,然后从总的数据中排除已经看过的数据,剩下的数据取前n条
例:每页5条数据,要看第三页
select top 5 * from Contacts    where contactId not in (select top ((3-1)*5) contactId from Contacts order by contactId asc) order by contactId asc

分页思路2

1.先为每条记录增加一个行号 row_number()
2.通过用户要查看的数据,使用row_number进行删选
3.例子:从(2*5)+1到3*5
select * from(
select *,rn=row_number() over(order by contactId asc) from Contacts)as t
where t.rn between (2*5)+1 and 3*5
--------------------------------------------------
数据库参数,第几页,每页多少条
数据库返回总页数,总条数

PagerHelper类

    /// <summary>
    /// 
    /// </summary>
    /// <param name="intCounts">总条数</param>
    /// <param name="intPageSizes">每页的条数</param>
    /// <param name="intPageCounts">总页数</param>
    /// <param name="intThisPages">第几页</param>
    /// <param name="strUrl">页码跳转的超链接,如UrlName.ashx?pageIndex="1"&pageSize="5"</param>
    /// <returns>html字符串,用于动态生成前台页面</returns>
    public static string strPage(int intCounts, int intPageSizes, int intPageCounts, int intThisPages, string strUrl)
    {
        int intCount = Convert.ToInt32(intCounts); //总记录数
        int intPageCount = Convert.ToInt32(intPageCounts); //总共页数
        int intPageSize = Convert.ToInt32(intPageSizes); //每页显示
        int intPage = 7;  //数字显示
        int intThisPage = Convert.ToInt32(intThisPages); //当前页数
        int intBeginPage = 0; //开始页数
        int intCrossPage = 0; //变换页数
        int intEndPage = 0; //结束页数
        string strPage = null; //返回值

        intCrossPage = intPage / 2;
            strPage = "共 <font color=\"#FF0000\">" + intCount.ToString() + "</font> 条记录 第 <font color=\"#FF0000\">" + intThisPage.ToString() + "/" + intPageCount.ToString() + "</font> 页 每页 <font color=\"#FF0000\">" + intPageSize.ToString() + "</font> 条 &nbsp;&nbsp;&nbsp;&nbsp;";
            if (intThisPage > 1)
            {
                strPage = strPage + "<a href=\"" + strUrl + "1\">首页</a> ";
                strPage = strPage + "<a href=\"" + strUrl + Convert.ToString(intThisPage - 1) + "\">上一页</a> ";
            }
            if (intPageCount > intPage)
            {
                if (intThisPage > intPageCount - intCrossPage)
                {
                    intBeginPage = intPageCount - intPage + 1;
                    intEndPage = intPageCount;
                }
            else
            {
                if (intThisPage <= intPage - intCrossPage)
                {
                    intBeginPage = 1;
                    intEndPage = intPage;
                }
                else
                {
                    intBeginPage = intThisPage - intCrossPage;
                    intEndPage = intThisPage + intCrossPage;
                }
            }
        }
        else
        {
            intBeginPage = 1;
            intEndPage = intPageCount;
        }
        if (intCount > 0)
        {

            for (int i = intBeginPage; i <= intEndPage; i++)
            {
                if (i == intThisPage)
                {
                    strPage = strPage + " <font color=\"#FF0000\">" + i.ToString() + "</font> ";
                }
                else
                {
                    strPage = strPage + " <a href=\"" + strUrl + i.ToString() + "\" title=\"第" + i.ToString() + "页\">" + i.ToString() + "</a> ";
                }
            }
        }
        if (intThisPage < intPageCount)
        {
            strPage = strPage + "<a href=\"" + strUrl + Convert.ToString(intThisPage + 1) + "\">下一页</a> ";
            strPage = strPage + "<a href=\"" + strUrl + intPageCount.ToString() + "\">尾页</a> ";
        }
        return strPage;
    }
posted on 2017-04-19 17:30  NE_STOP  阅读(1)  评论(0编辑  收藏  举报  来源