Custom Paging in ASP.NET 2.0 with SQL Server 2005

In web application it is a routine to paging data retrieved from the database. And in ASP.NET 2.0 it is very simple to paging data by enable the "AllowPaging" property of GridView. This solution works well when the database data amount is not very large. However, if you want to page through thousands, tens of thousands, or hundreds of thousands
of records the default paging model is not viable.

1. Paging Data with SQL Server 200

create procedure [dbo].[P_GetPagedOrders2000]
(@startIndex int,
@endIndex int
)
as
set nocount on
declare @indextable table(id int identity(1,1),nid int)
set rowcount @endIndex
insert into @indextable(nid) select orderid from orders order by orderid desc
select O.orderid,O.orderdate,O.customerid,C.CompanyName,E.FirstName+' '+E.LastName as EmployeeName
from orders O
left outer join Customers C
on O.CustomerID=C.CustomerID
left outer join Employees E
on O.EmployeeID=E.EmployeeID
inner join @indextable t on
O.orderid=t.nid
where t.id between @startIndex and @endIndex order by t.id
set nocount off
RETURN

2. Paging Data with SQL Server 2005

In SQL Server 2005 it is more easier to page data using the keyword "ROW_NUMBER()". The code looks like below.

create PROCEDURE [dbo].[P_GetPagedOrders2005]
(@startIndex INT,
@endindex INT
)
AS
begin
WITH orderList AS (
SELECT ROW_NUMBER() OVER (ORDER BY O.orderid DESC)AS Row, O.orderid,O.orderdate,O.customerid,C.CompanyName,E.FirstName+' '+E.LastName as EmployeeName
from orders O
left outer join Customers C
on O.CustomerID=C.CustomerID
left outer join Employees E
on O.EmployeeID=E.EmployeeID)

SELECT orderid,orderdate,customerid,companyName,employeeName
FROM orderlist
WHERE Row between @startIndex and @endIndex
end

3. Make it More Generalization

In real application we often need to query data by joining multiple tables,and sorting by any field if possible. The one optimized shown as below.

CREATE Procedure [ISIS].[GetPagingData]
(
@PageIndex int, -- count from 0
@PageSize int, -- record amount shown per page
@SortExpression nvarchar(100), -- Sorting Fields, include ASC or Desc
@TableOrViewName nvarchar(500), -- table or view name
@FieldList nvarchar(2000), -- Fields to Return:* for all
@Filter nvarchar(1000) -- string for where clause
)
AS
Begin
SET NOCOUNT ON
Declare @SQL nvarchar(2000)
Declare @TotalQuery nvarchar(2000)

Set @SQL = '
Select * From (
Select ' + @FieldList + ',
ROW_NUMBER() OVER (ORDER BY ' + @SortExpression + ') as RowNum
FROM ' + @TableOrViewName + ' Where 1=1 And ' + @Filter + '
) as PagedList
WHERE RowNum >= ' + cast(@PageIndex * @PageSize + 1 as nvarchar(6)) + ' AND RowNum <= ' + cast((@PageIndex + 1) * @PageSize as nvarchar(6))

Set @TotalQuery = '
Select count(*)
FROM ' + @TableOrViewName + ' Where 1=1 And ' + @Filter

EXEC sp_executesql @SQL
EXEC sp_executesql @TotalQuery


print @SQL
print @TotalQuery

End

References:

http://www.4guysfromrolla.com/articles/032206-1.aspx

http://blog.guohai.org/?p=460

[@more@]

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/13651903/viewspace-1037112/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/13651903/viewspace-1037112/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值