文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>Custom Paging in ASP.NET 2.0 with SQL Server 2005

Custom Paging in ASP.NET 2.0 with SQL Server 2005

时间:2010-08-20  来源:yuzhangqi

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


相关阅读 更多 +
排行榜 更多 +
辰域智控app

辰域智控app

系统工具 下载
网医联盟app

网医联盟app

运动健身 下载
汇丰汇选App

汇丰汇选App

金融理财 下载