最近给客户做了个查看通话记录的小功能,不论是查询还是显示都很快的完成了。唯一的问题就是因为数据记录太多[上万条,且每天都已几十近百条的速度增长],显示速度太慢。我用的是VS2005 GridView自带的分页功能,显然数据太多GridView自身的分页功能已经不在适用。本来想用存储过程,但感觉太麻烦。在网上淘了半天,终于淘了个非常好的利用SQLServer2005自带的Row_Number()函数做的SQL语句分页的方法。闲话少说,看代码:
1
declare
@pageIndex
int
2 declare @pageSize int
3 set @pageIndex = 1
4 set @pageSize = 26
5
6 SELECT * FROM ( SELECT ROW_NUMBER() OVER ( ORDER BY id DESC ) AS rownum,
7 id, callerno, calleeno, uid, callbegin,callend,bs
8 FROM [ 222.43.48.95 ] .lyzj.dbo.billlog where uid = ' 007 ' ) AS D
9 WHERE rownum BETWEEN ( @pageIndex - 1 ) * @pageSize + 1 AND @pageIndex * @pageSize
10 ORDER BY id DESC
上面就是分页的SQL语句,但是实际使用的时候,问题又出现了。我用的是ASPNETPager分页控件,查看的时候,第一页的数据总是无法显示;调试了半天才找出原因。原来是早先用GridView自身的分页时,分页的索引是从0开始的,而现在的分页却是从1 开始。两者搅在一起,不出问题都怪了,呵呵,搞定!
2 declare @pageSize int
3 set @pageIndex = 1
4 set @pageSize = 26
5
6 SELECT * FROM ( SELECT ROW_NUMBER() OVER ( ORDER BY id DESC ) AS rownum,
7 id, callerno, calleeno, uid, callbegin,callend,bs
8 FROM [ 222.43.48.95 ] .lyzj.dbo.billlog where uid = ' 007 ' ) AS D
9 WHERE rownum BETWEEN ( @pageIndex - 1 ) * @pageSize + 1 AND @pageIndex * @pageSize
10 ORDER BY id DESC