先來(lái)說(shuō)說(shuō)實(shí)現(xiàn)方式:
1、我們來(lái)假定Table中有一個(gè)已經(jīng)建立了索引的主鍵字段ID(整數(shù)型),我們將按照這個(gè)字段來(lái)取數(shù)據(jù)進(jìn)行分頁(yè)。
2、頁(yè)的大小我們放在@PageSize中
3、當(dāng)前頁(yè)號(hào)我們放在@CurrentPage中
4、如何讓記錄指針快速滾動(dòng)到我們要取的數(shù)據(jù)開(kāi)頭的那一行呢,這是關(guān)鍵所在!有了Set RowCount,我們就很容易實(shí)現(xiàn)了。
5、如果我們成功地滾動(dòng)記錄指針到我們要取的數(shù)據(jù)的開(kāi)頭的那一行,然后我們把那一行的記錄的ID字段的值記錄下來(lái),那么,利用Top和條件,我們就很容易的得到指定頁(yè)的數(shù)據(jù)了。當(dāng)然,有了Set RowCount,我們難道還用Top么?
看看Set Rowcount怎么來(lái)幫我們的忙吧:
復(fù)制代碼 代碼如下:
Declare @ID int
Declare @MoveRecords int
--@CurrentPage和@PageSize是傳入?yún)?shù)
Set @MoveRecords=@CurrentPage * @PageSize+1
--下面兩行實(shí)現(xiàn)快速滾動(dòng)到我們要取的數(shù)據(jù)的行,并把ID記錄下來(lái)
Set Rowcount @MoveRecords
Select @ID=ID from Table1 Order by ID
Set Rowcount @PageSize
--最恨為了減少麻煩使用*了,但是在這里為了說(shuō)明方便,暫時(shí)用一下
Select * From Table1 Where ID>=@ID Order By ID
Set Rowcount 0
大家可以試試看,在一個(gè)1千W記錄的表里面,一下子方翻頁(yè)到第100頁(yè)(每頁(yè)100條),看看有多快!
因?yàn)槠匠?倒序 排的比較多,上面也很好改。
將 Order by ID 改成 Order by ID DESC
將 Where ID>=@ID Order By ID 改成 Where ID=@ID Order By ID DESC
就可以了.
復(fù)制代碼 代碼如下:
set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
GO
Create PROCEDURE [dbo].[Tag_Page_Name_Select]
-- 傳入最大顯示紀(jì)錄數(shù)和當(dāng)前頁(yè)碼
@MaxPageSize int,
@PageNum int,
-- 設(shè)置一個(gè)輸出參數(shù)返回總紀(jì)錄數(shù)供分頁(yè)列表使用
@Count int output
AS
BEGIN
SET NOCOUNT ON;
DECLARE
-- 定義排序名稱(chēng)參數(shù)
@Name nvarchar(50),
-- 定義游標(biāo)位置
@Cursor int
-- 首先得到紀(jì)錄總數(shù)
Select @Count = count(tag_Name)
FROM [viewdatabase0716].[dbo].[view_tag];
-- 定義游標(biāo)需要開(kāi)始的位置
Set @Cursor = @MaxPageSize*(@PageNum-1)+1
-- 如果游標(biāo)大于紀(jì)錄總數(shù)將游標(biāo)放到最后一頁(yè)開(kāi)始的位置
IF @Cursor > @Count
BEGIN
-- 如果最后一頁(yè)與最大每次紀(jì)錄數(shù)相等,返回最后整頁(yè)
IF @Count % @MaxPageSize = 0
BEGIN
IF @Cursor > @MaxPageSize
Set @Cursor = @Count - @MaxPageSize + 1
ELSE
Set @Cursor = 1
END
-- 否則返回最后一頁(yè)剩下的紀(jì)錄
ELSE
Set @Cursor = @Count - (@Count % @MaxPageSize) + 1
END
-- 將指針指到該頁(yè)開(kāi)始
Set Rowcount @Cursor
-- 得到紀(jì)錄開(kāi)始的位置
Select @Name = tag_Name
FROM [viewdatabase0716].[dbo].[view_tag]
orDER BY tag_Name;
-- 設(shè)置開(kāi)始位置
Set Rowcount @MaxPageSize
-- 得到該頁(yè)紀(jì)錄
Select *
From [viewdatabase0716].[dbo].[view_tag]
Where tag_Name >= @Name
order By tag_Name
Set Rowcount 0
END
分頁(yè)控件
復(fù)制代碼 代碼如下:
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>
/// 擴(kuò)展連接字符串
/// /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>
/// 得到生成的分頁(yè)HTML
/// /summary>
public string GetPageHtml
{
get
{
return PageString.ToString();
}
}
/// summary>
/// 設(shè)置或獲取目前頁(yè)數(shù)
/// /summary>
public int PageNum
{
get
{
return PrivatePageNum;
}
set
{
if (value >= 1)
{
PrivatePageNum = value;
}
}
}
/// summary>
/// 設(shè)置或獲取最大分頁(yè)數(shù)
/// /summary>
public int MaxPageSize
{
get
{
return PrivateMaxPageSize;
}
set
{
if (value >= 1)
{
PrivateMaxPageSize = value;
}
}
}
/// summary>
/// 設(shè)置或獲取每次顯示最大頁(yè)數(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ù)總頁(yè)數(shù)
/// /summary>
public int AllPage
{
get
{
return PrivateAllPage;
}
}
/// summary>
/// 初始化分頁(yè)
/// /summary>
public void Pagination()
{
PageString = new StringBuilder("");
//得到總頁(yè)數(shù)
PrivateAllPage = (int)Math.Ceiling((decimal)PrivateCount / (decimal)PrivateMaxPageSize);
//防止上標(biāo)或下標(biāo)越界
if (PrivatePageNum > PrivateAllPage)
{
PrivatePageNum = PrivateAllPage;
}
//滾動(dòng)游標(biāo)分頁(yè)方式
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;
}
}
//生成頁(yè)碼列表統(tǒng)計(jì)
PageString.Append(...);
StringBuilder PreviousString = new StringBuilder("");
//如果在第一頁(yè)
if (PrivatePageNum > 1)
{
...
}
else
{
...
}
//如果在第一組分頁(yè)
if (PrivatePageNum > PrivateMaxPages)
{
...
}
else
{
...
}
PageString.Append(PreviousString);
//生成中間頁(yè)
for (int i = LeftStart; i = RightEnd; i++)
{
//為當(dāng)前頁(yè)時(shí)
if (i == PrivatePageNum)
{
...
}
else
{
...
}
}
StringBuilder LastString = new StringBuilder("");
//如果在最后一頁(yè)
if (PrivatePageNum PrivateAllPage)
{
...
}
else
{
...
}
//如果在最后一組
if ((PrivatePageNum + PrivateMaxPages) PrivateAllPage)
{
...
}
else
{
...
}
PageString.Append(LastString);
}
/// summary>
/// 生成Tag分類(lèi)表格
/// /summary>
public void TagTable(ExDataRow myExDataRow)
{
InsertString.Append(...);
}
調(diào)用方法:
//得到分頁(yè)設(shè)置并放入Session
ExRequest myExRequest = new ExRequest();
myExRequest.PageSession("Tag_", new string[] { "page", "size" });
//生成Tag分頁(yè)
ExStringBuilder Tag = new ExStringBuilder();
//設(shè)置每次顯示多少條紀(jì)錄
Tag.MaxPageSize = Convert.ToInt32(Session["Tag_size"]);
//設(shè)置最多顯示多少頁(yè)碼
Tag.MaxPages = 9;
//設(shè)置當(dāng)前為第幾頁(yè)
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)用存儲(chǔ)過(guò)程
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[i]));
}
TagBox.InnerHtml = Tag.GetHtml;
您可能感興趣的文章:- 真正高效的SQLSERVER分頁(yè)查詢(多種方案)
- oracle,mysql,SqlServer三種數(shù)據(jù)庫(kù)的分頁(yè)查詢的實(shí)例
- sqlserver分頁(yè)的兩種寫(xiě)法分別介紹
- sqlserver2005使用row_number() over分頁(yè)的實(shí)現(xiàn)方法
- sqlserver 三種分頁(yè)方式性能比較[圖文]
- sqlserver 千萬(wàn)數(shù)量級(jí)分頁(yè)存儲(chǔ)過(guò)程代碼
- sqlserver 存儲(chǔ)過(guò)程分頁(yè)(按多條件排序)
- SQL Server 2012使用Offset/Fetch Next實(shí)現(xiàn)分頁(yè)數(shù)據(jù)查詢