码迷,mamicode.com
首页 > 其他好文 > 详细

分页整理

时间:2015-06-18 13:03:07      阅读:77      评论:0      收藏:0      [点我收藏+]

标签:

直接使用DataTble分页代码:

 /// <summary>
    /// DataTable分页
    /// </summary>
    /// <param name="dt">DataTable</param>
    /// <param name="PageIndex">页索引,注意:从1开始</param>
    /// <param name="PageSize">每页大小</param>
    /// <returns>分好页的DataTable数据</returns>              第1页        每页10条
    public static DataTable GetPagedTable(DataTable dt, int PageIndex, int PageSize)
    {
        if (PageIndex == 0) { return dt; }
        DataTable newdt = dt.Copy();
        newdt.Clear();
        int rowbegin = (PageIndex - 1) * PageSize;
        int rowend = PageIndex * PageSize;

        if (rowbegin >= dt.Rows.Count)
        { return newdt; }

        if (rowend > dt.Rows.Count)
        { rowend = dt.Rows.Count; }
        for (int i = rowbegin; i <= rowend - 1; i++)
        {
            DataRow newdr = newdt.NewRow();
            DataRow dr = dt.Rows[i];
            foreach (DataColumn column in dt.Columns)
            {
                newdr[column.ColumnName] = dr[column.ColumnName];
            }
            newdt.Rows.Add(newdr);
        }
        return newdt;
    }

大数据分页存储过程,修改重复项分页不显示。

ALTER PROCEDURE [dbo].[pager] 
          (
             @tablename    nvarchar(4000),        --表名
    @strGetFields nvarchar(4000) = *,  --查询列名
             @PageIndex    int,                --当前页
             @PageSize    int,                    --每页记录数
             @strWhere  nvarchar(4000) = ‘‘,     --查询条件
    @strOrder    nvarchar(500),        --排序字段,多字段必须要接ASC和DESC
    @intOrder bit = 0,        --排序类型  1为升序   0为降序
    @CountAll bigint output              --返回纪录总数用于计算页面数   
)
AS
DECLARE @sqlstr        nvarchar(4000)

DECLARE @TableId    int                --表ID



SET @TableId = OBJECT_ID(@tablename)
IF @TableId IS NULL
BEGIN
    RAISERROR(表名或对象名不存在当前数据库中,请检查, 16, 1)
    RETURN
END

--设置查询条件
IF LEN(@strWhere)>0
set @strWhere=  WHERE   +@strWhere


--取得总记录数
SET @sqlstr=SELECT @iRowCount=COUNT(*) FROM +@tablename+ +@strWhere
EXEC SP_EXECUTESQL @sqlstr, N@iRowCount int OUTPUT, @CountAll OUTPUT


--正反排序

    DECLARE @new_order2 VARCHAR(1000)      --正排序
IF LEN(@strOrder)>0
BEGIN
     SET @new_order2 =  orDER BY  + @strOrder               
       IF charindex(,,@strOrder)>0
    BEGIN

            SET @new_order2 = @strOrder + ,      
                 IF @intOrder = 1          
                        BEGIN          
                    SET @new_order2 = REPLACE(REPLACE(@new_order2,ASC,,{ASC},),DESC,,{DESC},)          
                    SET @new_order2 = REPLACE(REPLACE(@new_order2,{ASC},,DESC,),{DESC},,ASC,)           
                        END  

     
             SET @new_order2 =  orDER BY  + SUBSTRING(@new_order2,1,LEN(@new_order2)-1)   
              

    END
    ELSE
    BEGIN
         IF @intOrder = 1          
                    BEGIN          
                        SET @new_order2 =  orDER BY  + @strOrder +  DESC    
                   
                    END    
    END
--插入SQL
set  @sqlstr= select top +cast((@PageIndex*@PageSize) as nvarchar)+ +@strGetFields+ ,row_identityid = IDENTITY(int,1,1) INTO #TEMP    from ( select top +cast((@PageIndex*@PageSize) as nvarchar)+ +@strGetFields+ from  +@tablename+  +@strWhere+  +@new_order2+ )a   select  +@strGetFields+   from #TEMP   where row_identityid> +cast((@PageSize*(@PageIndex-1)) as nvarchar)+  drop table #TEMP

END

ELSE
    BEGIN
--插入SQL
set  @sqlstr= select top +cast((@PageIndex*@PageSize) as nvarchar)+ +@strGetFields+ ,row_identityid = IDENTITY(int,1,1) INTO #TEMP    from ( select top +cast((@PageIndex*@PageSize) as nvarchar)+ +@strGetFields+ from  +@tablename+  +@strWhere+  )a   select  +@strGetFields+   from #TEMP   where row_identityid> +cast((@PageSize*(@PageIndex-1)) as nvarchar)+  drop table #TEMP

END


--print @CountAll
--print @sqlstr
EXEC(@sqlstr)

 

分页整理

标签:

原文地址:http://www.cnblogs.com/loyung/p/4585473.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!