第一种:最笨的方法,设置DataGrid的AllowPaging属性为true,设置PageSize属性,
然后编写OnPageIndexChange方法,设置所在页,重新获取DataSource的全部数据。
这种方法在大数据量时延迟很大,也不易于服务器程序的扩展,不过特点就是代码
量小,适合小数据不想做特殊处理的应用。
第二种:设置DataGrid的AllowPaging和AllowCustomPaging的属性为true,允许自定义
分页,设置PageSize,同时动态设置VirtualItemCount,来让DataGrid自动产生底部的
分页导航栏。也要自己处理OnPageIndexChange方法,不过这里涉及到的方法很多,大概
归结如下:
1.读取DataSource的全部数据,自己处理Cache,每次根据请求的页号给DataGrid特定
的数据。在数据量超大的时候,也没有想到很好的方法,可以缓存大量数据,而且对
于更新频繁的数据,这种缓存的方法,还要考虑一致性的问题。
2.采用数据库相关的方法,每次获取最多pagenumber*PageSize条记录,然后根据页号
获取PageSize条或者最后的不足PageSize条记录。sql server采取top关键字,oracle
则采用rownum或者rowid自动生成列。这个也缺乏扩展性,在数据量超大的时候效率
低下。如共400万条记录,获取从第200万开始的连续10条记录,仍然很慢。
3.采取数据库相关的方法,每次获取恰好的PageSize条记录,赋给DataGrid,这个根据
不同的数据库,存在不同的方法。我只介绍oracle和sql server的方法:
a. oracle的方法:
select * from (select rownum as row_id, xxx, yyy from (select xxx, yyy
from any_table [where ...][group by ...][order by ...])) where row_id
between ((pagenumber-1)*PageSize+1) and pagenumber*PageSize
b. sql server 7.0以上的方法
b.1 使用存储过程:
CREATE PROCEDURE sp_FetchRecordByPage
(@selectCmd varchar(128), @PageNumber smallint, @PageSize smallint)
AS
-- @selectCmd, use to create cursor
-- @PageNumber, page to fetch
-- @PageSize, records that fit into one page
-- internal variables
DECLARE @MyIndex smallint
DECLARE @FirstRecPos smallint
-- sets the position of the first record to
-- retrieve in the page
SET @FirstRecPos = (@PageNumber-1) * @PageSize + 1
-- cursor associated with the query string
EXEC('DECLARE select_cursor SCROLL CURSOR FOR ' + @selectCmd)
-- opens the cursor
OPEN select_cursor
-- fetches the first record in the page
FETCH ABSOLUTE @FirstRecPos FROM select_cursor
-- loops through the cursor to pick up the
-- other records in the page
SET @MyIndex = 1
WHILE @@FETCH_STATUS = 0 AND @MyIndex < @PageSize
BEGIN
FETCH NEXT FROM select_cursor
SET @MyIndex = @MyIndex + 1
END
-- clean up
CLOSE select_cursor
DEALLOCATE select_cursor
GO
b.2使用sql语句,sql server 7.0以上支持top关键字
SELECT * FROM
(SELECT TOP pagenumber*PageSize * FROM anytable
[where ...][group by ...][order by ...]) AS t1
WHERE NOT EXISTS
(SELECT * FROM (SELECT TOP (pagenumber-1)*Pagesize *
FROM anytable [where ...][group by ...][order by ...])
AS t2 WHERE t1.xxx=t2.xxx)
xxx是表的主键,适合有主键的任何表
问题如下:大数据量时哪种方法效率最高?个人感觉是采用存储过程,内部使用光标
的方法,这个对于oracle也可以采用,不过不一定比oracle的select效率高,而且光标
方法返回的是多个记录集,每个记录集一条记录,需要用DataReader读取后组装成
DataTable或者DataSet然后作为数据源赋给DataGrid,不过也可以直接把DataReader
赋给DataGrid,记得及时关闭数据链接就可以了
--
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请
点击举报。