-- ============================================= -- Create date: <2012-9-12> -- Description: <高效分页存储过程,适用于Sql2005> -- Notes: <排序字段强烈建议建索引>
-- sosoft.cnblogs.com -- ============================================= create Procedure [dbo].[Page] @TableName varchar(50), --表名 @Fields varchar(1000) = '*', --字段名(全部字段为*) @OrderField varchar(1000), --排序字段(必须!支持多字段) @sqlWhere varchar(1000) = Null,--条件语句(不用加where) @pageSize int, --每页多少条记录 @pageIndex int = 1 , --指定当前为第几页 @TotalPage int output --返回总页数 as begin Begin Tran --开始事务 Declare @sql nvarchar(4000); Declare @totalRecord int; --计算总记录数 if (@SqlWhere='' or @sqlWhere=NULL) set @sql = 'select @totalRecord = count(*) from ' + @TableName else set @sql = 'select @totalRecord = count(*) from ' + @TableName + ' where ' + @sqlWhere EXEC sp_executesql @sql,N'@totalRecord int OUTPUT',@totalRecord OUTPUT--计算总记录数 --计算总数量 select @TotalPage=@totalRecord--CEILING((@totalRecord+0.0)/@PageSize)+1 if (@SqlWhere='' or @sqlWhere=NULL) set @sql = 'Select * FROM (select ROW_NUMBER() Over(order by ' + @OrderField + ') as rowId,' + @Fields + ' from ' + @TableName else set @sql = 'Select * FROM (select ROW_NUMBER() Over(order by ' + @OrderField + ') as rowId,' + @Fields + ' from ' + @TableName + ' where ' + @SqlWhere --处理页数超出范围情况 if @PageIndex<=0 Set @pageIndex = 1 if @pageIndex>@TotalPage Set @pageIndex = @TotalPage --处理开始点和结束点 Declare @StartRecord int Declare @EndRecord int set @StartRecord = (@pageIndex-1)*@PageSize + 1 set @EndRecord = @StartRecord + @pageSize - 1 --继续合成sql语句 set @Sql = @Sql + ') as ' + @TableName + ' where rowId between ' + Convert(varchar(50),@StartRecord) + ' and ' + Convert(varchar(50),@EndRecord) --print @Sql Exec(@Sql) -- print @totalRecord If @@Error <> 0 Begin RollBack Tran Return -1 End Else Begin Commit Tran --print @totalRecord Return @totalRecord ---返回记录总数 End end
下面是C#函数代码
#region 获取分页的数据 /// <summary> /// 获取分页的数据 /// </summary> /// <param name="TblName">数据表名</param> /// <param name="Fields">要读取的字段</param> /// <param name="OrderField">排序字段</param> /// <param name="SqlWhere">查询条件</param> /// <param name="PageSize">每页显示多少条数据</param> /// <param name="pageIndex">当前页码</param> /// <param name="TotalPage">返回值,共有多少页</param> /// <returns></returns>
/// sosoft.cnblogs.com
public static DataSet PageData(string TblName, string Fields, string OrderField, string SqlWhere, int PageSize, int pageIndex, out int TotalPage) { TotalPage = 1; DbObject db = new DbObject(); string connString = db.ConnectionString; SqlConnection conn = new SqlConnection(connString); SqlCommand comm = new SqlCommand("Page", conn); comm.Parameters.Add(new SqlParameter("@TableName", SqlDbType.NVarChar, 100)); comm.Parameters[0].Value = TblName; comm.Parameters.Add(new SqlParameter("@Fields", SqlDbType.NVarChar, 1000)); comm.Parameters[1].Value = Fields; comm.Parameters.Add(new SqlParameter("@OrderField", SqlDbType.NVarChar, 1000)); comm.Parameters[2].Value = OrderField; comm.Parameters.Add(new SqlParameter("@sqlWhere", SqlDbType.NVarChar, 1000)); comm.Parameters[3].Value = SqlWhere; comm.Parameters.Add(new SqlParameter("@pageSize", SqlDbType.Int)); comm.Parameters[4].Value = PageSize; comm.Parameters.Add(new SqlParameter("@pageIndex", SqlDbType.Int)); comm.Parameters[5].Value = pageIndex; comm.Parameters.Add(new SqlParameter("@TotalPage", SqlDbType.Int)); comm.Parameters[6].Direction = ParameterDirection.Output; comm.CommandType = CommandType.StoredProcedure; SqlDataAdapter dataAdapter = new SqlDataAdapter(comm); DataSet ds = new DataSet(); dataAdapter.Fill(ds); TotalPage = (int)comm.Parameters[6].Value; conn.Close(); conn.Dispose(); comm.Dispose(); db.Dispose(); return ds; } #endregion