现在我有个分页的存储过程,并且查询数据的时候已经对数据进行了相关的处理,我在用datagird进行显示的时候,请问,存储过程计算的总页数等相关参数是怎么取出来的??

解决方案 »

  1.   

    SELECT TOP 10 * FROM jobs WHERE (job_id NOT IN (SELECT TOP 12 job_id FROM jobs ORDER BY job_id)) ORDER BY job_id 
    建立表: CREATE TABLE [TestTable] ( 
    [ID] [int] IDENTITY (1, 1) NOT NULL , 
    [FirstName] [nvarchar] (100) COLLATE Chinese_PRC_CI_AS NULL , 
    [LastName] [nvarchar] (100) COLLATE Chinese_PRC_CI_AS NULL , 
    [Country] [nvarchar] (50) COLLATE Chinese_PRC_CI_AS NULL , 
    [Note] [nvarchar] (2000) COLLATE Chinese_PRC_CI_AS NULL 
    ) ON [PRIMARY] 
    GO 插入数据:(2万条,用更多的数据测试会明显一些) 
    SET IDENTITY_INSERT TestTable ON declare @i int 
    set @i=1 
    while @i<=20000 
    begin 
    insert into TestTable([id], FirstName, LastName, Country,Note) values(@i, 'FirstName_XXX','LastName_XXX','Country_XXX','Note_XXX') 
    set @i=@i+1 
    end SET IDENTITY_INSERT TestTable OFF  ------------------------------------- 分页方案一:(利用Not In和SELECT TOP分页) 
    语句形式: 
    SELECT TOP 10 * 
    FROM TestTable 
    WHERE (ID NOT IN 
    (SELECT TOP 20 id 
    FROM TestTable 
    ORDER BY id)) 
    ORDER BY ID 
    SELECT TOP 页大小 * 
    FROM TestTable 
    WHERE (ID NOT IN 
    (SELECT TOP 页大小*页数 id 
    FROM 表 
    ORDER BY id)) 
    ORDER BY ID ------------------------------------- 分页方案二:(利用ID大于多少和SELECT TOP分页) 
    语句形式: 
    SELECT TOP 10 * 
    FROM TestTable 
    WHERE (ID > 
    (SELECT MAX(id) 
    FROM (SELECT TOP 20 id 
    FROM TestTable 
    ORDER BY id) AS T)) 
    ORDER BY ID 
    SELECT TOP 页大小 * 
    FROM TestTable 
    WHERE (ID > 
    (SELECT MAX(id) 
    FROM (SELECT TOP 页大小*页数 id 
    FROM 表 
    ORDER BY id) AS T)) 
    ORDER BY ID 
    ------------------------------------- 分页方案三:(利用SQL的游标存储过程分页) 
    create procedure XiaoZhengGe 
    @sqlstr nvarchar(4000), --查询字符串 
    @currentpage int, --第N页 
    @pagesize int --每页行数 
    as 
    set nocount on 
    declare @P1 int, --P1是游标的id 
    @rowcount int 
    exec sp_cursoropen @P1 output,@sqlstr,@scrollopt=1,@ccopt=1,@rowcount=@rowcount output 
    select ceiling(1.0*@rowcount/@pagesize) as 总页数--,@rowcount as 总行数,@currentpage as 当前页 
    set @currentpage=(@currentpage-1)*@pagesize+1 
    exec sp_cursorfetch @P1,16,@currentpage,@pagesize 
    exec sp_cursorclose @P1 
    set nocount off 其它的方案:如果没有主键,可以用临时表,也可以用方案三做,但是效率会低。 
    建议优化的时候,加上主键和索引,查询效率会提高。 通过SQL 查询分析器,显示比较:我的结论是: 
    分页方案二:(利用ID大于多少和SELECT TOP分页)效率最高,需要拼接SQL语句 
    分页方案一:(利用Not In和SELECT TOP分页) 效率次之,需要拼接SQL语句 
    分页方案三:(利用SQL的游标存储过程分页) 效率最差,但是最为通用 在实际情况中,要具体分析。
      

  2.   

    我的意思是说,我已经有这个存储过程了,但是我绑定上datagird后?怎么取出这个pagecount(总页数)  ???
      

  3.   

    -- =============================================
    -- Author: <Author,,Name>
    -- Create date: <Create Date,,>
    -- Description: <Description,,>
    -- =============================================
    CREATE procedure [dbo].[sp_PageChange]
             @PageIndex INT,                             
             @PageSize  INT,                             
             @RecordCount INT OUT,                
             @PageCount INT OUT,                  
             @strGetFields nvarchar(1000),        
             @tableName nvarchar(500) ,           
             @ID nvarchar(100),                   
             @strWhere  nvarchar(1000) ='',       
             @sortName nvarchar(50) =' asc ' ,    
             @orderName nvarchar(100)             AS
    declare @countSelect nvarchar(2000)  
    --设置统计查询语句
    if len(@strWhere) =0 
    --如果没有查询条件
        begin
            set @countSelect=N'SELECT @CountRecord = COUNT(*)  FROM '+@tableName
        end
    else
    --否则
        begin
            set @countSelect=N'SELECT @CountRecord = COUNT(*)  FROM '+@tableName+' where '+@strWhere
        end
    --执行并返回总数
    exec sp_executesql @countSelect,N'@CountRecord int output',@RecordCount output
    SET @PageCount = CEILING(@RecordCount * 1.0 / @PageSize) SET NOCOUNT ONDECLARE @SQLSTR NVARCHAR(3000)
    --实际总共的页码小于当前页码 或者 最大页码
    if @PageCount>=0
        --如果分页后页数大于0
        begin
            if @PageCount<=@PageIndex and  @PageCount>0   --如果实际总共的页数小于datagrid索引的页数
                --or @PageCount=1
                begin
                    --设置为最后一页
       set @PageIndex=@PageCount-1
                end
            else if @PageCount<=@PageIndex and  @PageCount=0
                begin
                    set @PageIndex=0;
                end
        end
    --如果用普通的sql而不使用存储过程调用
    declare @ID_temp varchar(100)
    set @ID_temp='cast('+@ID+' as nvarchar(100)) '
    declare @returnValue nvarchar(100)
    set @returnValue=','''+cast(@RecordCount as nvarchar(100)) +'|'+cast(@PageCount as nvarchar(100))+'|'+'''+'+@ID_temp+' as [returnValue] '
    --如果用普通的sql而不使用存储过程调用
    IF @PageIndex = 0 OR @PageCount <= 1  --如果为第一页
        begin
            if len(@strWhere) =0
                begin
                    SET @SQLSTR =N'SELECT TOP '+STR( @PageSize )+@strGetFields+@returnValue+' FROM  '+@tableName+' ORDER BY '+@orderName+@sortName
                end
            else
                begin
                    SET @SQLSTR =N'SELECT TOP '+STR( @PageSize )+@strGetFields+@returnValue+'  FROM  '+@tableName+' where '+@strWhere+' ORDER BY '+@orderName+@sortName
                end
        end
    ELSE IF     @PageIndex = @PageCount - 1 --如果为最后一页            
        begin
            if len(@strWhere) =0
                begin
                    SET @SQLSTR =N' SELECT '+@strGetFields+@returnValue+'  FROM '+@tableName+' where '+@ID+' not in  ( SELECT TOP '+STR(/*@RecordCount - */@PageSize * @PageIndex )+@ID+'  FROM  '+@tableName+' ORDER BY '+@orderName+@sortName+' )   ORDER BY '+@orderName+@sortName
                end
            else
                begin
                    SET @SQLSTR =N' SELECT '+@strGetFields+@returnValue+'  FROM '+@tableName+' where '+@ID+' not in  ( SELECT TOP '+STR(/*@RecordCount - */ @PageSize * @PageIndex )+@ID+'  FROM  '+@tableName+' where '+@strWhere+' ORDER BY '+@orderName+@sortName+' )  and '+@strWhere+' ORDER BY '+@orderName+@sortName
                end
        end
    ELSE                                                              --否则执行  
        begin
             if len(@strWhere) =0
                begin
                    SET @SQLSTR =N' SELECT TOP  '+STR( @PageSize )+@strGetFields+@returnValue+' FROM '+@tableName+' where '+@ID+' not in  ( SELECT TOP '+STR( /*@RecordCount - */@PageSize * @PageIndex )+' '+@ID+'  FROM  '+@tableName+' ORDER BY '+@orderName+@sortName+' )  ORDER BY '+@orderName+@sortName
                end
             else
                begin
                    SET @SQLSTR =N' SELECT TOP  '+STR( @PageSize )+@strGetFields+@returnValue+' FROM '+@tableName+' where '+@ID+' not in  (SELECT TOP '+STR(/*@RecordCount - */ @PageSize * @PageIndex )+@ID+'  FROM  '+@tableName+' where '+@strWhere+' ORDER BY '+@orderName+@sortName+' )and '+@strWhere+'ORDER BY '+@orderName+@sortName
                end
        end
    EXEC (@SQLSTR)set nocount offGO用这个存储过程试试看,它的第三个参数就是总页数
      

  4.   

    楼主你的意思是自己有个存储过程能实现分页的功能?
    我不知道你的存储过程是怎么样的?你可以在存储过程中增加一个输出参数
    @PageCount int OutPut
    在存储过程中给输出参数赋值,在调用存储过程后,就可以在程序中取出
    这个参数的值了
      

  5.   

    qiushuangqun(探索是一种精神,值得表扬!) 在存储过程中你可以返回两个表,一个表示总记录数,一个表是要显示的数据
    然后再程序中获取这两个表就可以了。这样的话,我把存储过程的结果放进DataSet的两个表,会不会明显影响性能?