【问题标题】:Implement Paging With SQL Server 2008 in MVC3 C#在 MVC3 C# 中使用 SQL Server 2008 实现分页
【发布时间】:2014-02-12 13:32:18
【问题描述】:

目前我有一个前端 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 行。如果 page no = 1 从位置 200 到 400 的行将被返回,依此类推。

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

【问题讨论】:

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


【解决方案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

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

希望这会有所帮助..

【讨论】:

  • Avinash 你能修改上面的内容以包括我的 Select 语句和我上面的问题的连接。谢谢
猜你喜欢
  • 1970-01-01
  • 2015-09-29
  • 1970-01-01
  • 1970-01-01
  • 2011-06-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多