千万级数据库分页 与aspnetpager 的联合应用

aspnetpager:其实只是起到用户界面的作用,记录了用户对页码的选择

并且传递给存储过程。如图:

//分页用到存储过程、不需要改变的存储过程

//只是以后要用到的

Create procedure jxytext_pagination @sqlstr nvarchar(4000), --查询字符串 @currentpage int, --第N页 @pagesize int --每页行数 as set nocount on declare @P1 int, --P1是游标的id @rowcount int exec sp_cursoropen @P1 output,@sqlstr,@scrollopt=1,@ccopt=1,@rowcount=@rowcount output select ceiling(1.0*@rowcount/@pagesize) as TotalPages,@rowcount as TotalRows,@currentpage as CurPage set @currentpage=(@currentpage-1)*@pagesize+1 exec sp_cursorfetch @P1,16,@currentpage,@pagesize exec sp_cursorclose @P1 set nocount off GO

//真正要用到的存储过程

create procedure Pr_jxytest3 ( @sql varchar(1000) , @indexpage int, @pagesize int ) AS begin exec jxytext_pagination @sql,@indexpage,@pagesize end GO

//aspp.net页面

using System; using System.Data; using System.Configuration; using System.Collections; 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 DAL; using System.Data.SqlClient; public partial class Default6 : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { bangding(); } public void bangding() { string sql = "select * from room"; //以下是参数,有两个是靠AspNetPager1传给par的 //真正的参数只有sql,也正是用户所用到的查询字符串 SqlParameter[] par = new SqlParameter[3]; par[0] = new SqlParameter("@sql", SqlDbType.VarChar); par[0].Value=sql; par[1] = new SqlParameter("@indexpage", SqlDbType.Int); par[1].Value = this.AspNetPager1.CurrentPageIndex; par[2] = new SqlParameter("@pagesize", SqlDbType.Int); par[2].Value = this.AspNetPager1.PageSize; DataSet Ds = SqlHelper.ExecuteDataset(SqlHelper.CONNECTION_STRING, CommandType.StoredProcedure, "Pr_jxytest3",par); //绑定的地方Tables[2]是记录集 //Tables[1]是记录信息 this.GridView1.DataSource = Ds.Tables[2]; this.GridView1.DataBind(); //下面这一句是为分页控件付总的记录条数 this.AspNetPager1.RecordCount =Convert.ToInt32(Ds.Tables[1].Rows[0][1]); } protected void AspNetPager1_PageChanged(object sender, EventArgs e) { bangding(); } }//前台页面
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default6.aspx.cs" Inherits="Default6" %>



<%@ Register Assembly="AspNetPager" Namespace="Wuqi.Webdiyer" TagPrefix="webdiyer" %>



<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">



<html xmlns="http://www.w3.org/1999/xhtml" >

<head runat="server">

    <title>无标题页</title>

</head>

<body>

    <form id="form1" runat="server">

        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">

            <Columns>

                <asp:BoundField DataField="roomname" HeaderText="房源名称" />

            </Columns>

        </asp:GridView>

        <webdiyer:aspnetpager id="AspNetPager1" runat="server" onpagechanged="AspNetPager1_PageChanged"

            pagesize="5" urlpaging="True"></webdiyer:aspnetpager>



    </form>

</body>

</html>
到此已经实现了分页,效率很高的
阅读更多

没有更多推荐了,返回首页