首页 > 资讯列表 > 编程/数据库 >> 数据库操作教程

ASP.NET和MSSQL高性能分页实例代码

数据库操作教程 2022-09-23 17:34:41 转载来源: 网络整理/侵权必删

首先是存储过程,只取出我需要的那段数据,如果页数超过数据总数,自动返回最后一页的纪录:setANSI_NULLSONsetQUOTED_IDENTIFIERONGO--=============================================--Author:Clear--Description:高性能分页--http://www.cnblogs

首先是存储过程,只取出我需要的那段数据,如果页数超过数据总数,自动返回最后一页的纪录:

set ANSI_NULLS ONset QUOTED_IDENTIFIER ONGO-- =============================================-- Author: Clear-- Description: 高性能分页-- http://www.cnblogs.com/roucheng/-- =============================================Alter PROCEDURE [dbo].[Tag_Page_Name_Select]-- 传入最大显示纪录数和当前页码@MaxPageSize int,@PageNum int,-- 设置一个输出参数返回总纪录数供分页列表使用@Count int outputASBEGINSET 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 > @CountBEGIN-- 如果最后一页与最大每次纪录数相等,返回最后整页IF @Count % @MaxPageSize = 0BEGINIF @Cursor > @MaxPageSizeSet @Cursor = @Count - @MaxPageSize + 1ELSESet @Cursor = 1END-- 否则返回最后一页剩下的纪录ELSESet @Cursor = @Count - (@Count % @MaxPageSize) + 1END-- 将指针指到该页开始Set Rowcount @Cursor-- 得到纪录开始的位置Select @Name = tag_NameFROM [viewdatabase0716].[dbo].[view_tag]orDER BY tag_Name;-- 设置开始位置Set Rowcount @MaxPageSize-- 得到该页纪录Select * From [viewdatabase0716].[dbo].[view_tag]Where tag_Name >= @Nameorder By tag_NameSet Rowcount 0END 

然后是分页控件(... 为省略的生成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>/// 设置或获取目前页数/// </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);//生成中间页 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(...);} 

调用方法:

//得到分页设置并放入SessionExRequest 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));}TagBox.InnerHtml = Tag.GetHtml; 

标签: ASP.NET MSSQL 高性能 分页 实例 代码


声明:本文内容来源自网络,文字、图片等素材版权属于原作者,平台转载素材出于传递更多信息,文章内容仅供参考与学习,切勿作为商业目的使用。如果侵害了您的合法权益,请您及时与我们联系,我们会在第一时间进行处理!我们尊重版权,也致力于保护版权,站搜网感谢您的分享!

站长搜索

http://www.adminso.com

Copyright @ 2007~2024 All Rights Reserved.

Powered By 站长搜索

打开手机扫描上面的二维码打开手机版


使用手机软件扫描微信二维码

关注我们可获取更多热点资讯

站长搜索目录系统技术支持