直接使用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)