当数据库的数据量比较大,对执行效率要求比较高的时候,我们可以考虑使用存储过程来实现分页,根据传入的页数返回需要显示的数据表,仅仅select出当前页的数据。(这个比使用PagedDataSource类而言效率要高。)
现在采用Repeater来实现一个数据分页,数据库采用SQL server2000,利用里面的系统表Northwind。
新建存储过程如下:
create PROCEDURE dbo.myPaging
(
@pagesize int,
@currentPage int,
@total int output
)
AS
create table #temp
(
ID int identity(1,1),
CustomerID varchar(50),
CompanyName varchar(50),
ContactName varchar(50),
ContactTitle varchar(50),
Phone varchar(50)
)
insert into #temp(CustomerID,CompanyName,ContactName,ContactTitle,Phone)
select CustomerID,CompanyName,ContactName,ContactTitle,Phone
from Customers
select @total=(select count(*) from Customers)
declare @startID int
declare @endID int
set @startID=(@currentpage-1)*@pagesize+1
set @endID=@currentpage*@pagesize
select * from #temp where ID>=@startID and ID<=@endID
GO
若不会写存储过程的话,可以参照网站在线生成分页的存储过程:http://www.webdiyer.com/AspNetPager/utility/sqlspgen.aspx
前台代码:
后台代码如下:
感谢你阅读本文,希望这篇文章给你带来帮助!