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

一、存储过程如下:

 

 1
 2
 3 --  =============================================
 4 --  Author:  普向东
 5 --  Create date: 2008-7-25
 6 --  Description: 适用于SQL 2005通用分页存储过程
 7 --  =============================================
 8 ALTER   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 )
18 AS
19 BEGIN
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 ;
48 END
49
50
51
52

 

 

二、调用方法类:

 

  1 ExpandedBlockStart.gif ContractedBlock.gif /**/ /// <summary>
  2    /// 分页类DALDALPageHelper 的摘要说明
  3    /// </summary>

  4      public   class  DALPageHelper
  5 ExpandedBlockStart.gifContractedBlock.gif     {
  6ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <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)
 21ExpandedSubBlockStart.gifContractedSubBlock.gif        {
 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
 40ExpandedSubBlockStart.gifContractedSubBlock.gif            {
 41                recordCount = GetDateCount(connectionString, tableName, conditionName);
 42                SqlDataReader sdr;
 43ExpandedSubBlockStart.gifContractedSubBlock.gif                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
 49ExpandedSubBlockStart.gifContractedSubBlock.gif            {
 50                recordCount = 0;
 51                return null;
 52            }

 53        }

 54
 55ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <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)
 70ExpandedSubBlockStart.gifContractedSubBlock.gif        {
 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
 87ExpandedSubBlockStart.gifContractedSubBlock.gif            {
 88                DataSet ds = new DataSet();
 89ExpandedSubBlockStart.gifContractedSubBlock.gif                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
 95ExpandedSubBlockStart.gifContractedSubBlock.gif            {
 96                recordCount = 0;
 97                return null;
 98            }

 99        }

100
101
102ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <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)
111ExpandedSubBlockStart.gifContractedSubBlock.gif        {
112            try
113ExpandedSubBlockStart.gifContractedSubBlock.gif            {
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;
118ExpandedSubBlockStart.gifContractedSubBlock.gif                return (int)SqlHelper.ExecuteScalar(conn, CommandType.StoredProcedure, "PrcPageDateTotal"new SqlParameter[] { _tblName, _where });
119            }

120            catch
121ExpandedSubBlockStart.gifContractedSubBlock.gif            {
122                return 0;
123            }

124        }

125    }

126

 

转载于:https://www.cnblogs.com/oriental/articles/1254294.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值