简体   繁体   English

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

[英]Implement Paging With SQL Server 2008 in MVC3 C#

Currently I have a frontend JQuery datatable (which is set to serverside), and my stored procedure (SP) is returning all of the data as below: 当前,我有一个前端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

I want to achieve paging and restrict to only 200 rows of data at a time. 我想实现分页并且一次只能限制200行数据。 I can pass in the page no (page no begins from 0) as parameter to SP ie for example if the pageno = 0 the first 200 rows should only be returned. 我可以将页面编号(页面编号从0开始)作为参数传递给SP,例如,如果pageno = 0,则仅返回前200行。 if page no = 1 rows from position 200 to 400 will be returned and so on. 如果页面编号=从位置200到400的1行将被返回,依此类推。

How can I change the above SP to achieve this ? 如何更改上述SP来实现此目的?

If you want to retrieve data based on each page request , you can use the following stored proc: 如果要基于每个页面请求检索数据,则可以使用以下存储的proc:

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

Here, you need to send the parameters current page index that you are requesting , page size that is number of records per page 在这里,您需要发送参数当前请求的页面索引页面大小每页的记录数

Hope this helps.. 希望这可以帮助..

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM