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:
[@more@]來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/13651903/viewspace-1037112/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- Paging Records Using SQL Server 2005 Database - ROW_NUMBER FunctionSQLServerDatabaseFunction
- SQL server 2005 expressSQLServerExpress
- Sql Server 2005函式SQLServer函式
- SQL SERVER 2005 配置-saSQLServer
- SQL SERVER2005建Link ServerSQLServer
- Sql Server 2005新增T-sql特性SQLServer
- XML Support in Microsoft SQL Server 2005XMLROSSQLServer
- PowerShell連線 SQL Server 2005SQLServer
- Monitoring Tempdb in SQL Server 2005SQLServer
- 微軟SQL Server 2005速成版微軟SQLServer
- SQL Server2005快捷鍵SQLServer
- SQL SERVER 2005映象實驗SQLServer
- SQL Server 2005 Service Broker 初探SQLServer
- SQL Server 2005 Express Edition 概述SQLServerExpress
- SQL Server 2005:清空plan cacheSQLServer
- SQL Server 2005中修改 Server Collation的方法SQLServer
- asp.net連線資料庫(SQL Server 2005 Express)詳細說明ASP.NET資料庫SQLServerExpress
- sql server 2005資料庫快照SQLServer資料庫
- 配置SQL Server 2005伺服器SQLServer伺服器
- SQL SERVER 2005表分割槽功能SQLServer
- SQL Server 2005動態管理物件SQLServer物件
- 實戰 SQL Server 2005 映象配置SQLServer
- Sql server 2005中output用法解析SQLServer
- Sql Server 2005 日誌壓縮SQLServer
- SQL SERVER 2005 日誌收縮SQLServer
- sql server 2005使用點滴(1)SQLServer
- SQL Server 2005 Cluster 叢集部署SQLServer
- SQL Server 2005鎖的問題SQLServer
- 已安裝 SQL Server 2005 Express 工具。若要繼續,請刪除 SQL Server 2005 Express 工具SQLServerExpress
- SQL Server 2005效能調整二(zt)SQLServer
- SQL Server 2005效能調整一(zt)SQLServer
- 清除 SQL SERVER 2005 事務日誌SQLServer
- SQL Server 2005命令提示實用工具SQLServer
- SQL Server 2005 Beta 2 快照隔離SQLServer
- 修改SQL Server 2005執行環境SQLServer
- SQL server 2005 備份恢復模式SQLServer模式
- SQL Server 2005 功能比較 和 限制SQLServer
- SQL Server 2005分割槽表例項SQLServer