在查询gridview分页时设置限制

时间:2012-12-05 12:52:50

标签: asp.net sql-server-2008

我正在使用C#和SQL Server 2008,这里我附上了我的查询。我想设置动态网格视图的限制。我需要这个查询在动态网格视图上进行分页我怎么能

SELECT 
    Item.ItemID,Item.TypeID,Item.StatusID,Item.SeverityID,Item.PriorityID,Item.ProjectID,Item.ReleaseID,
    Item.ClientID,Item.Body,Item.CreatedBy,Item.CreatedDate,Item.ModifiedBy,Item.ModifiedDate,Item.IsActive, 
item.[Subject], Type.Name as Type, status.Name as Status, Project.Name as Project,Release.Name as Release,
Priority.Name as Priority,Severity.Name as Severity,Client.Name as Client,
ROW_NUMBER() OVER (ORDER BY ItemID) AS Rowis

FROM Item (NOLOCK)  
LEFT OUTER JOIN  [Type] (NOLOCK)  ON Item.TypeID = [Type].TypeID
LEFT OUTER JOIN  [Status] (NOLOCK) ON Item.StatusID = [Status].StatusID
LEFT OUTER JOIN [Project] (NOLOCK) ON Item.ProjectID=[Project].ProjectID
LEFT OUTER JOIN [Release] (NOLOCK) ON Item.ReleaseID =[Release].ReleaseID 
LEFT OUTER JOIN [Priority] (NOLOCK) ON Item.PriorityID=[Priority].PriorityID 
LEFT OUTER JOIN [Severity] (NOLOCK) ON Item.SeverityID =[Severity].SeverityID
LEFT OUTER JOIN [Client] (NOLOCK) ON Item.ClientID =[Client].ClientID 

1 个答案:

答案 0 :(得分:1)

@PageIndex和@PageSize是从应用程序层发送的参数。

DECLARE @PageIndex INT = 1
DECLARE @PageSize INT = 10

SELECT * FROM 
(
    SELECT 
        Item.ItemID,Item.TypeID,Item.StatusID,Item.SeverityID,Item.PriorityID,Item.ProjectID,Item.ReleaseID,
        Item.ClientID,Item.Body,Item.CreatedBy,Item.CreatedDate,Item.ModifiedBy,Item.ModifiedDate,Item.IsActive, 
    item.[Subject], Type.Name as Type, status.Name as Status, Project.Name as Project,Release.Name as Release,
    Priority.Name as Priority,Severity.Name as Severity,Client.Name as Client,
    ROW_NUMBER() OVER (ORDER BY ItemID) AS Rowis

    FROM Item (NOLOCK)  
    LEFT OUTER JOIN  [Type] (NOLOCK)  ON Item.TypeID = [Type].TypeID
    LEFT OUTER JOIN  [Status] (NOLOCK) ON Item.StatusID = [Status].StatusID
    LEFT OUTER JOIN [Project] (NOLOCK) ON Item.ProjectID=[Project].ProjectID
    LEFT OUTER JOIN [Release] (NOLOCK) ON Item.ReleaseID =[Release].ReleaseID 
    LEFT OUTER JOIN [Priority] (NOLOCK) ON Item.PriorityID=[Priority].PriorityID 
    LEFT OUTER JOIN [Severity] (NOLOCK) ON Item.SeverityID =[Severity].SeverityID
    LEFT OUTER JOIN [Client] (NOLOCK) ON Item.ClientID =[Client].ClientID 
)
AS Results
WHERE Rowis BETWEEN (@PageIndex - 1) * @PageSize + 1 AND @PageIndex * @PageSize --Paging