asp.net使用转发器分页

时间:2011-11-14 13:44:32

标签: asp.net sql sql-server-2005

我想为我的reapeater创建分页。在数据库中我有帖子。我想首先显示已登录的用户帖子,然后显示其余帖子(例如每个站点40个)。

我想知道是否有办法从40到80或80到120的数据库行进行检索。

感谢任何提示。

1 个答案:

答案 0 :(得分:0)

我们假设您的表格包含以下列:ID, UserID, PostedDate, Subject 然后查询可能如下所示:

DECLARE @LoggedInUserID int,
        @RowsPerPage int,
        @PageNum int;

SELECT  @LoggedInUserID = 1,
        @RowsPerPage = 10,
        @PageNum = 1; 

with BlogPostCTE
AS
(
    select 
    bp.ID,
    bp.UserID,
    bp.PostedDate,
    CASE WHEN bp.UserID = @LoggedInUserID THEN 1 ELSE 0 END as IsLoggedInUser,
    bp.[Subject]
    from BlogPost as bp
), BlogPostWithPagingCTE
AS
(
    Select Top(@RowsPerPage * @PageNum)
    ResultNum = ROW_NUMBER() OVER (ORDER BY IsLoggedInUser desc, UserID, PostedDate asc),
    ID,
    UserID,
    PostedDate,
    IsLoggedInUser,
    [Subject]
    FROM BlogPostCTE
)

select
    ID,
    UserID,
    PostedDate,
    [Subject]
FROM BlogPostWithPagingCTE
where ResultNum > ((@PageNum - 1) * @RowsPerPage)