在MVC3 C#中实现使用SQL Server 2008的分页

时间:2014-02-12 13:32:19

标签: c# jquery sql-server asp.net-mvc-3 tsql

目前我有一个前端JQuery数据表(设置为服务器端),我的存储过程(SP)返回所有数据,如下所示:

Select ID, FullName, FullAddress, Coord
From Names n
inner join Person p ON n.ID = p.PID
inner join Address a ON n.AddressID = a.ID
order by FullName

我想实现分页,一次只限制200行数据。我可以将页面号(页面号从0开始)作为参数传递给SP,即例如,如果pageno = 0,则只应返回前200行。如果页面否=将返回从位置200到400的1行,依此类推。

如何更改上述SP才能实现此目的?

1 个答案:

答案 0 :(得分:1)

如果要根据每个页面请求检索数据,可以使用以下存储过程:

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================

CREATE PROCEDURE GetDataPageWise                      // Name of the stored procedure
      @PageIndex INT = 1
      ,@PageSize INT = 10
      ,@RecordCount INT OUTPUT
AS
BEGIN
      SET NOCOUNT ON;

Select Row_Number() Over
(
 ID
) as RowNumber
ID, FullName, FullAddress, Coord
into #Results// #Results is the temporary table that we are creating
From Names n
inner join Person p ON n.ID = p.PID
inner join Address a ON n.AddressID = a.ID
order by FullName
SELECT @RecordCount = COUNT(*)FROM #Results

SELECT * FROM #Results
      WHERE RowNumber BETWEEN(@PageIndex -1) * @PageSize + 1 AND(((@PageIndex -1) * @PageSize + 1) + @PageSize) - 1

      DROP TABLE #Results                      // Dropping the temporary table results as it is not required furthur
END
GO

在这里,您需要发送您要请求的当前页面索引参数,页面大小,即每页的记录数

希望这会有所帮助..