sqlserver 高機能分頁完成剖析。本站提示廣大學習愛好者:(sqlserver 高機能分頁完成剖析)文章只能為提供參考,不一定能成為您想要的結果。以下是sqlserver 高機能分頁完成剖析正文
先來講說完成方法:
1、我們來假定Table中有一個曾經樹立了索引的主鍵字段ID(整數型),我們將依照這個字段來取數據停止分頁。
2、頁的年夜小我們放在@PageSize中
3、以後頁號我們放在@CurrentPage中
4、若何讓記載指針疾速轉動到我們要取的數據開首的那一行呢,這是症結地點!有了Set RowCount,我們就很輕易完成了。
5、假如我們勝利地轉動記載指針到我們要取的數據的開首的那一行,然後我們把那一行的記載的ID字段的值記載上去,那末,應用Top和前提,我們就很輕易的獲得指定頁的數據了。固然,有了Set RowCount,我們豈非還用Top麼?
看看Set Rowcount怎樣來幫我們的忙吧:
Declare @ID int
Declare @MoveRecords int
--@CurrentPage和@PageSize是傳入參數
Set @MoveRecords=@CurrentPage * @PageSize+1
--上面兩行完成疾速轉動到我們要取的數據的行,並把ID記載上去
Set Rowcount @MoveRecords
Select @ID=ID from Table1 Order by ID
Set Rowcount @PageSize
--最恨為了削減費事應用*了,然則在這裡為了解釋便利,臨時用一下
Select * From Table1 Where ID>=@ID Order By ID
Set Rowcount 0
年夜家可以嘗嘗看,在一個1千W記載的內外面,一會兒方翻頁到第100頁(每頁100條),看看有多快!
由於平凡 倒序 排的比擬多,下面也很好改。
將 Order by ID 改成 Order by ID DESC
將 Where ID>=@ID Order By ID 改成 Where ID<=@ID Order By ID DESC
便可以了.
set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
GO
Create PROCEDURE [dbo].[Tag_Page_Name_Select]
-- 傳入最年夜顯示記載數和以後頁碼
@MaxPageSize int,
@PageNum int,
-- 設置一個輸入參數前往總記載數供分頁列表應用
@Count int output
AS
BEGIN
SET NOCOUNT ON;
DECLARE
-- 界說排序稱號參數
@Name nvarchar(50),
-- 界說游標地位
@Cursor int
-- 起首獲得記載總數
Select @Count = count(tag_Name)
FROM [viewdatabase0716].[dbo].[view_tag];
-- 界說游標須要開端的地位
Set @Cursor = @MaxPageSize*(@PageNum-1)+1
-- 假如游標年夜於記載總數將游標放到最初一頁開端的地位
IF @Cursor > @Count
BEGIN
-- 假如最初一頁與最年夜每次記載數相等,前往最初整頁
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;
-- 設置開端地位
Set Rowcount @MaxPageSize
-- 獲得該頁記載
Select *
From [viewdatabase0716].[dbo].[view_tag]
Where tag_Name >= @Name
order By tag_Name
Set Rowcount 0
END
分頁控件
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>
/// 設置或獲得今朝頁數
/// </summary>
public int PageNum
{
get
{
return PrivatePageNum;
}
set
{
if (value >= 1)
{
PrivatePageNum = value;
}
}
}
/// <summary>
/// 設置或獲得最年夜分頁數
/// </summary>
public int MaxPageSize
{
get
{
return PrivateMaxPageSize;
}
set
{
if (value >= 1)
{
PrivateMaxPageSize = value;
}
}
}
/// <summary>
/// 設置或獲得每次顯示最年夜頁數
/// </summary>
public int MaxPages
{
get
{
return PrivateMaxPages;
}
set
{
PrivateMaxPages = value;
}
}
/// <summary>
/// 設置或獲得數據總數
/// </summary>
public int DateCount
{
get
{
return PrivateCount;
}
set
{
PrivateCount = value;
}
}
/// <summary>
/// 獲得數據總頁數
/// </summary>
public int AllPage
{
get
{
return PrivateAllPage;
}
}
/// <summary>
/// 初始化分頁
/// </summary>
public void Pagination()
{
PageString = new StringBuilder("");
//獲得總頁數
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;
}
}
//生成頁碼列表統計
PageString.Append(...);
StringBuilder PreviousString = new StringBuilder("");
//假如在第一頁
if (PrivatePageNum > 1)
{
...
}
else
{
...
}
//假如在第一組分頁
if (PrivatePageNum > PrivateMaxPages)
{
...
}
else
{
...
}
PageString.Append(PreviousString);
//生成中央頁
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(...);
}
挪用辦法:
//獲得分頁設置並放入Session
ExRequest myExRequest = new ExRequest();
myExRequest.PageSession("Tag_", new string[] { "page", "size" });
//生成Tag分頁
ExStringBuilder Tag = new ExStringBuilder();
//設置每次顯示若干條記載
Tag.MaxPageSize = Convert.ToInt32(Session["Tag_size"]);
//設置最多顯示若干頁碼
Tag.MaxPages = 9;
//設置以後為第幾頁
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()}
};
//挪用存儲進程
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;