c#存储过程实现分页数据读取 适用于SQL 2005,性能良好

一、存储过程如下:

 

 1
 2
 3-- =============================================
 4-- Author:  普向东
 5-- Create date: 2008-7-25
 6-- Description: 适用于SQL 2005通用分页存储过程
 7-- =============================================
 8ALTER PROCEDURE [dbo].[PrcPagination]
 9(    
10    @TableName        NVARCHAR(500),                -- 表名或视图名,甚至可以是嵌套SQL:(Select * From Tab Where ID>1000) Tab
11    @FieldsName        NVARCHAR(300= '*',        -- 返回记录集字段名,","隔开,默认是"*"
12    @ConditionName    NVARCHAR(500),                -- 过滤条件
13    @OrderBy        NVARCHAR(100= 'ID ASC',    -- 排序规则
14    @PageSize        INT = 10,                    -- 每页记录条数(页面大小)
15    @PageIndex        INT = 1,                    -- 当前页码
16    @TotalCount        INT OUTPUT                    -- 记录总数 
17)
18AS
19BEGIN
20    SET ROWCOUNT @PageSize;
21
22    BEGIN Try
23      -- 整合SQL
24      Declare @SQL NVARCHAR(4000), @Portion NVARCHAR(4000);
25            
26      SET @Portion = ' ROW_NUMBER() OVER (ORDER BY ' + @OrderBy + ') AS ROWNUM FROM ' + @TableName;
27
28      SET @Portion = @Portion + (CASE WHEN LEN(@ConditionName>= 1 THEN (' Where ' + @ConditionName + ') AS tab'ELSE (') AS tab'END);
29
30      SET @SQL = 'Select TOP(' + CAST(@PageSize AS NVARCHAR(8)) + '' + @FieldsName + ' FROM (Select ' + @FieldsName + ',' + @Portion;
31
32      SET @SQL = @SQL + ' Where tab.ROWNUM > ' + CAST((@PageIndex-1)*@PageSize AS NVARCHAR(8));
33    
34        print @SQL
35      -- 执行SQL, 取当前页记录集
36      Execute(@SQL);
37      --------------------------------------------------------------------
38      -- 计算数据记录总量
39      SET @SQL = 'SET @Rows = (Select MAX(ROWNUM) FROM (Select' + @Portion + ')';
40      Execute sp_executesql @SQL, N'@Rows INT OUTPUT'@TotalCount OUTPUT
41     
42    End Try
43    BEGIN Catch
44    End Catch;
45
46    -- 执行成功
47    Return @TotalCount;
48END
49
50
51
52

 

 

二、调用方法类:

 

  1/// <summary>
  2    /// 分页类DALDALPageHelper 的摘要说明
  3    /// </summary>

  4    public class DALPageHelper
  5    {
  6        /// <summary>
  7        /// 分页数据读取,返回类型: SqlDataReader
  8        /// by pxd in 2008-7-23
  9        /// </summary>
 10        /// <param name="connectionString">数据库连接字符串</param>
 11        /// <param name="tableName">数据表名称</param>
 12        /// <param name="primaryKey">数据主键字段</param>
 13        /// <param name="fieldsName">检索字段</param>
 14        /// <param name="conditionName">检索条件,Where 条件</param>
 15        /// <param name="orderByName">排序字符串</param>
 16        /// <param name="pageSize">每页数据量</param>
 17        /// <param name="pageIndex">当前页码</param>
 18        /// <param name="recordCount">数据总量</param>
 19        /// <returns>分页数据读取,返回类型: SqlDataReader</returns>

 20        public static SqlDataReader GetPageDataSdr(string connectionString, string tableName, string fieldsName, string conditionName, string orderByName, int pageSize, int pageIndex, out int recordCount)
 21        {
 22            SqlParameter _tblName = new SqlParameter("TableName", SqlDbType.VarChar, 500);
 23            _tblName.Value = tableName;
 24            SqlParameter _fields = new SqlParameter("FieldsName", SqlDbType.VarChar, 1000);
 25            _fields.Value = fieldsName;
 26            SqlParameter _condition = new SqlParameter("ConditionName", SqlDbType.VarChar, 255);
 27            _condition.Value = conditionName;
 28            SqlParameter _orderBy = new SqlParameter("OrderBy", SqlDbType.VarChar, 255);
 29            _orderBy.Value = orderByName;
 30            SqlParameter _pageSize = new SqlParameter("PageSize", SqlDbType.Int, 4);
 31            _pageSize.Value = pageSize;
 32            SqlParameter _pageIndex = new SqlParameter("PageIndex", SqlDbType.Int, 4);
 33            _pageIndex.Value = pageIndex;
 34            SqlParameter _rCount = new SqlParameter("TotalCount", SqlDbType.Int, 4);
 35            _rCount.Direction = ParameterDirection.Output;
 36
 37
 38
 39            try
 40            {
 41                recordCount = GetDateCount(connectionString, tableName, conditionName);
 42                SqlDataReader sdr;
 43                sdr = SqlHelper.ExecuteReader(connectionString, CommandType.StoredProcedure, "PrcPagination"new SqlParameter[] { _tblName, _fields, _condition, _orderBy, _pageSize, _pageIndex, _rCount });
 44               // recordCount = Convert.ToInt32(_rCount.Value);
 45                return sdr;
 46
 47            }

 48            catch
 49            {
 50                recordCount = 0;
 51                return null;
 52            }

 53        }

 54
 55        /// <summary>
 56        /// 分页数据读取,返回类型: DataSet
 57        /// by pxd in 2008-7-23
 58        /// </summary>
 59        /// <param name="connectionString">数据库连接字符串</param>
 60        /// <param name="tableName">数据表名称</param>
 61        /// <param name="primaryKey">数据主键字段</param>
 62        /// <param name="fieldsName">检索字段</param>
 63        /// <param name="conditionName">检索条件,Where 条件</param>
 64        /// <param name="orderByName">排序字符串</param>
 65        /// <param name="pageSize">每页数据量</param>
 66        /// <param name="pageIndex">当前页码</param>
 67        /// <param name="recordCount">数据总量</param>
 68        /// <returns>分页数据读取,返回类型: SqlDataReader</returns>

 69        public static DataSet GetPageDataDS(string connectionString, string tableName, string fieldsName, string conditionName, string orderByName, int pageSize, int pageIndex, out int recordCount)
 70        {
 71            SqlParameter _tblName = new SqlParameter("TableName", SqlDbType.VarChar, 50);
 72            _tblName.Value = tableName;
 73            SqlParameter _fields = new SqlParameter("FieldsName", SqlDbType.VarChar, 1000);
 74            _fields.Value = fieldsName;
 75            SqlParameter _condition = new SqlParameter("ConditionName", SqlDbType.VarChar, 255);
 76            _condition.Value = conditionName;
 77            SqlParameter _orderBy = new SqlParameter("OrderBy", SqlDbType.VarChar, 255);
 78            _orderBy.Value = orderByName;
 79            SqlParameter _pageSize = new SqlParameter("PageSize", SqlDbType.Int, 4);
 80            _pageSize.Value = pageSize;
 81            SqlParameter _pageIndex = new SqlParameter("PageIndex", SqlDbType.Int, 4);
 82            _pageIndex.Value = pageIndex;
 83            SqlParameter _rCount = new SqlParameter("TotalCount", SqlDbType.Int, 4);
 84            _rCount.Direction = ParameterDirection.Output;
 85
 86            try
 87            {
 88                DataSet ds = new DataSet();
 89                ds = SqlHelper.ExecuteDataset(connectionString, CommandType.StoredProcedure, "PrcPagination"new SqlParameter[] { _tblName, _fields, _condition, _orderBy, _pageSize, _pageIndex, _rCount });
 90                recordCount = Convert.ToInt32(_rCount.Value);
 91                return ds;
 92
 93            }

 94            catch
 95            {
 96                recordCount = 0;
 97                return null;
 98            }

 99        }

100
101
102        /// <summary>
103        /// 得到分页数据总量
104        /// by pxd in 2008-7-23
105        /// </summary>
106        /// <param name="conn">数据库连接字符串</param>
107        /// <param name="tableName">数据表名</param>
108        /// <param name="conditionName">检索条件</param>
109        /// <returns></returns>

110        public static int GetDateCount(string conn, string tableName, string conditionName)
111        {
112            try
113            {
114                SqlParameter _tblName = new SqlParameter("TableName", SqlDbType.VarChar, 255);
115                _tblName.Value = tableName;
116                SqlParameter _where = new SqlParameter("ConditionName", SqlDbType.VarChar, 255);
117                _where.Value = conditionName;
118                return (int)SqlHelper.ExecuteScalar(conn, CommandType.StoredProcedure, "PrcPageDateTotal"new SqlParameter[] { _tblName, _where });
119            }

120            catch
121            {
122                return 0;
123            }

124        }

125    }

126

 

posted on 2008-07-28 09:22  普向东的博客  阅读(1040)  评论(1编辑  收藏  举报

导航