首先是存儲過程,只取出我需要的那段數(shù)據(jù),如果頁數(shù)超過數(shù)據(jù)總數(shù),自動返回最后一頁的紀錄:
set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author: Clear
-- Description: 高性能分頁
-- http://www.cnblogs.com/roucheng/
-- =============================================
Alter PROCEDURE [dbo].[Tag_Page_Name_Select]
-- 傳入最大顯示紀錄數(shù)和當前頁碼
@MaxPageSize int,
@PageNum int,
-- 設(shè)置一個輸出參數(shù)返回總紀錄數(shù)供分頁列表使用
@Count int output
AS
BEGIN
SET NOCOUNT ON;
DECLARE
-- 定義排序名稱參數(shù)
@Name nvarchar(50),
-- 定義游標位置
@Cursor int
-- 首先得到紀錄總數(shù)
Select @Count = count(tag_Name)
FROM [viewdatabase0716].[dbo].[view_tag];
-- 定義游標需要開始的位置
Set @Cursor = @MaxPageSize*(@PageNum-1)+1
-- 如果游標大于紀錄總數(shù)將游標放到最后一頁開始的位置
IF @Cursor > @Count
BEGIN
-- 如果最后一頁與最大每次紀錄數(shù)相等,返回最后整頁
IF @Count % @MaxPageSize = 0
BEGIN
IF @Cursor > @MaxPageSize
Set @Cursor = @Count - @MaxPageSize + 1
ELSE
Set @Cursor = 1
END
-- 否則返回最后一頁剩下的紀錄
ELSE
Set @Cursor = @Count - (@Count % @MaxPageSize) + 1
END
-- 將指針指到該頁開始
Set Rowcount @Cursor
-- 得到紀錄開始的位置
Select @Name = tag_Name
FROM [viewdatabase0716].[dbo].[view_tag]
orDER BY tag_Name;
-- 設(shè)置開始位置
Set Rowcount @MaxPageSize
-- 得到該頁紀錄
Select *
From [viewdatabase0716].[dbo].[view_tag]
Where tag_Name >= @Name
order By tag_Name
Set Rowcount 0
END
然后是分頁控件(... 為省略的生成HTML代碼方法):
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
/// summary>
/// 擴展連接字符串
/// /summary>
public class ExStringBuilder
{
private StringBuilder InsertString;
private StringBuilder PageString;
private int PrivatePageNum = 1;
private int PrivateMaxPageSize = 25;
private int PrivateMaxPages = 10;
private int PrivateCount;
private int PrivateAllPage;
public ExStringBuilder()
{
InsertString = new StringBuilder("");
}
/// summary>
/// 得到生成的HTML
/// /summary>
public string GetHtml
{
get
{
return InsertString.ToString();
}
}
/// summary>
/// 得到生成的分頁HTML
/// /summary>
public string GetPageHtml
{
get
{
return PageString.ToString();
}
}
/// summary>
/// 設(shè)置或獲取目前頁數(shù)
/// /summary>
public int PageNum
{
get
{
return PrivatePageNum;
}
set
{
if (value >= 1)
{
PrivatePageNum = value;
}
}
}
/// summary>
/// 設(shè)置或獲取最大分頁數(shù)
/// /summary>
public int MaxPageSize
{
get
{
return PrivateMaxPageSize;
}
set
{
if (value >= 1)
{
PrivateMaxPageSize = value;
}
}
}
/// summary>
/// 設(shè)置或獲取每次顯示最大頁數(shù)
/// /summary>
public int MaxPages
{
get
{
return PrivateMaxPages;
}
set
{
PrivateMaxPages = value;
}
}
/// summary>
/// 設(shè)置或獲取數(shù)據(jù)總數(shù)
/// /summary>
public int DateCount
{
get
{
return PrivateCount;
}
set
{
PrivateCount = value;
}
}
/// summary>
/// 獲取數(shù)據(jù)總頁數(shù)
/// /summary>
public int AllPage
{
get
{
return PrivateAllPage;
}
}
/// summary>
/// 初始化分頁
/// /summary>
public void Pagination()
{
PageString = new StringBuilder("");
//得到總頁數(shù)
PrivateAllPage = (int)Math.Ceiling((decimal)PrivateCount / (decimal)PrivateMaxPageSize);
//防止上標或下標越界
if (PrivatePageNum > PrivateAllPage)
{
PrivatePageNum = PrivateAllPage;
}
//滾動游標分頁方式
int LeftRange, RightRange, LeftStart, RightEnd;
LeftRange = (PrivateMaxPages + 1) / 2-1;
RightRange = (PrivateMaxPages + 1) / 2;
if (PrivateMaxPages >= PrivateAllPage)
{
LeftStart = 1;
RightEnd = PrivateAllPage;
}
else
{
if (PrivatePageNum = LeftRange)
{
LeftStart = 1;
RightEnd = LeftStart + PrivateMaxPages - 1;
}
else if (PrivateAllPage - PrivatePageNum RightRange)
{
RightEnd = PrivateAllPage;
LeftStart = RightEnd - PrivateMaxPages + 1;
}
else
{
LeftStart = PrivatePageNum - LeftRange;
RightEnd = PrivatePageNum + RightRange;
}
}
//生成頁碼列表統(tǒng)計
PageString.Append(...);
StringBuilder PreviousString = new StringBuilder("");
//如果在第一頁
if (PrivatePageNum > 1)
{
...
}
else
{
...
}
//如果在第一組分頁
if (PrivatePageNum > PrivateMaxPages)
{
...
}
else
{
...
}
PageString.Append(PreviousString);
//生成中間頁 http://www.cnblogs.com/roucheng/
for (int i = LeftStart; i = RightEnd; i++)
{
//為當前頁時
if (i == PrivatePageNum)
{
...
}
else
{
...
}
}
StringBuilder LastString = new StringBuilder("");
//如果在最后一頁
if (PrivatePageNum PrivateAllPage)
{
...
}
else
{
...
}
//如果在最后一組
if ((PrivatePageNum + PrivateMaxPages) PrivateAllPage)
{
...
}
else
{
...
}
PageString.Append(LastString);
}
/// summary>
/// 生成Tag分類表格
/// /summary>
public void TagTable(ExDataRow myExDataRow)
{
InsertString.Append(...);
}
調(diào)用方法:
//得到分頁設(shè)置并放入Session
ExRequest myExRequest = new ExRequest();
myExRequest.PageSession("Tag_", new string[] { "page", "size" });
//生成Tag分頁
ExStringBuilder Tag = new ExStringBuilder();
//設(shè)置每次顯示多少條紀錄
Tag.MaxPageSize = Convert.ToInt32(Session["Tag_size"]);
//設(shè)置最多顯示多少頁碼
Tag.MaxPages = 9;
//設(shè)置當前為第幾頁
Tag.PageNum = Convert.ToInt32(Session["Tag_page"]);
string[][] myNamenValue = new string[2][]{
new string[]{"MaxPageSize","PageNum","Count"},
new string[]{Tag.MaxPageSize.ToString(),Tag.PageNum.ToString()}
};
//調(diào)用存儲過程
DataTable myDataTable = mySQL.BatchGetDB("Tag_Page_Name_Select", myNamenValue, "Count");
Tag.DateCount = (int)mySQL.OutputCommand.Parameters["@Count"].Value;
Tag.Pagination();
HeadPage.InnerHtml = FootPage.InnerHtml = Tag.GetPageHtml;
for (int i = 0, j = myDataTable.Rows.Count; i j; i++)
{
Tag.TagTable(new ExDataRow(myDataTable.Rows));
}
TagBox.InnerHtml = Tag.GetHtml;
您可能感興趣的文章:- MS sqlserver 2008數(shù)據(jù)庫轉(zhuǎn)換成2000版本的方法
- MSSql簡單查詢出數(shù)據(jù)表中所有重復(fù)數(shù)據(jù)的方法
- MSSQL分頁存儲過程完整示例(支持多表分頁存儲)
- PHP封裝的MSSql操作類完整實例
- 拯救你的數(shù)據(jù) 通過日志恢復(fù)MSSQL數(shù)據(jù)
- MSSQL產(chǎn)生死鎖的根本原因及解決方法
- SQL(MSSQLSERVER)服務(wù)啟動錯誤代碼3414的解決方法
- PHP連接MSSQL方法匯總
- 如何處理Python3.4 使用pymssql 亂碼問題
- MS SQL 實現(xiàn)驗證字符串是否包含有大小寫字母的功能