web页面一次一般显示10行数据为好,但往往很多时候我们从数据库中查出来上万条数据,这个时候我们要在上万条数据中显示10条,那就必须分页。分页的第一问题就是分页数据。
www.mrw3c.com,专业的网页设计知识库。
分页数据
分页的数据分为两种,一种是在数据库中只取需要的10条数据,这也是性能提升的标致,一种是全盘拖出,放到程序缓存中再用程序来分页。
S(NNnH!r,PSRJW!ALS/7
第一种取数据方式:
- select top [PageSize] * from [Table] where id not in( select top [CurrentPage-1] [PageSize] id from [Table] )
用于access,mssql当中,另一种是存储过程,这也是最快的取数据方式
- ALTER PROCEDURE GetAuthors
- @PageIndex int,
- @PageSize int
- AS
- BEGIN
- -- Set the page bounds
- DECLARE @PageLowerBound int
- DECLARE @PageUpperBound int
- DECLARE @TotalRecords int
- SET @PageLowerBound = @PageSize * (@PageIndex-1)
- SET @PageUpperBound = @PageSize*@PageIndex-1
- -- Create a temp table TO store the select results
- CREATE TABLE #PageIndexForAuthors
- (
- IndexId int IDENTITY (0, 1) NOT NULL,
- au_id varchar(11),
- au_lname varchar(40),
- au_fname varchar(20),
- phone char(12),
- address varchar(40),
- city varchar(20),
- state char(2),
- zip char(5),
- contract bit
- )
- -- Insert into our temp table
- INSERT INTO #PageIndexForAuthors
- (au_id ,au_lname,au_fname,phone,address,city,state,zip,contract)
- SELECT *
- FROM authors
- ORDER BY au_id
- SELECT @TotalRecords = @@ROWCOUNT
- SELECT * FROM #PageIndexForAuthors
- WHERE IndexId between @PageLowerBound AND @PageUpperBound
- ORDER BY au_id
- RETURN @TotalRecords
- END
- GO
S(NNnH!r,PSRJW!ALS/7
上面这段存储过程是仿微软的写法,微软在Membership里面采用也是这存储过程分页。
其他数据库取数据也有不同,sql2005有行号函数,oracle也有,mysql就更好取了limit 10,20 mOHLqMTrjw[n$
第二种就是全部数据取出来再做缓存,数据用程序处理,.ado/jdbc有定位法,Adapter.Fill()用这个填充需要的数据,GridView自定义分页事件就是使用这个,看到很多自定义控件也使用这种方式