最近项目使用的SqLServer分页存储过程及调用封装代码

存储过程:

 

USE [RS]

GO



/****** Object:  StoredProcedure [dbo].[UP_Paging]    Script Date: 05/30/2013 17:43:23 ******/

SET ANSI_NULLS ON

GO



SET QUOTED_IDENTIFIER ON

GO





ALTER PROCEDURE [dbo].[UP_Paging]

	@Tables nvarchar(512), --表名,多张表是请使用 tA a inner join tB b On a.AID = b.AID

	@PK nvarchar(128)='',    --主键,可以带表头 a.AID

	@Sort nvarchar(512) = '', --排序字段

	@PageIndex int = 1,    --开始页码

	@PageSize int = 10,        --页大小

	@Fields nvarchar(1024) = '*',--读取字段

	@Where nvarchar(1024) = NULL,--Where条件

	@RecordCount int output --返回总条数

AS



DECLARE @strFilter nvarchar(4000)

declare @sql nvarchar(4000)

IF @Where IS NOT NULL AND @Where != ''

  BEGIN

   SET @strFilter = ' WHERE ' + @Where + ' '

  END

ELSE

  BEGIN

   SET @strFilter = ''

  END



if @Sort = ''

  set @Sort = @PK + ' DESC '



IF @PageIndex < 1

  SET @PageIndex = 1



if @PageIndex = 1 --第一页提高性能

begin 

  set @sql = 'select top ' + str(@PageSize) +' '+@Fields+ '  from ' + @Tables + ' ' + @strFilter + ' ORDER BY  '+ @Sort

  print @sql

end 

else

  begin  

	DECLARE @START_ID varchar(50)	--页开始索引

	DECLARE @END_ID varchar(50)		--页结束索引

	SET @START_ID = convert(varchar(50),(@PageIndex - 1) * @PageSize + 1)

	SET @END_ID = convert(varchar(50),@PageIndex * @PageSize)

	

	set @sql =  ' SELECT '+@Fields+ 

				' FROM '+

				' ('+

					' SELECT ROW_NUMBER() OVER(ORDER BY '+@Sort+') AS rownum, '+@Fields+ 

					' FROM '+@Tables+' ' +@strFilter+

				' ) AS D'+

				' WHERE rownum BETWEEN '+@START_ID+' AND ' +@END_ID +' ORDER BY '+@Sort

  END



EXEC (@sql)



--总条数

set @recordCount=0;

set @sql = N'SELECT  @recordCount=Count(1) FROM ' + @Tables + @strFilter

EXEC sp_executesql @sql,N'@recordCount int out',@RecordCount out



	



GO




C#调用代码:

 

 

/// <summary>

        /// 通用分页

        /// </summary>

        /// <param name="pi"></param>

        public virtual void Pager(RPageInfo pi)

        {

            /*存储过程

             CREATE PROCEDURE UP_Paging

	            @Tables nvarchar(512), --表名,多张表是请使用 tA a inner join tB b On a.AID = b.AID

	            @PK nvarchar(128)='',    --主键,可以带表头 a.AID

	            @Sort nvarchar(512) = '', --排序字段

	            @PageIndex int = 1,    --开始页码

	            @PageSize int = 10,        --页大小

	            @Fields nvarchar(1024) = '*',--读取字段

	            @Where nvarchar(1024) = NULL,--Where条件

	            @RecordCount int output --返回总条数

            AS

             */

            SqlParameter[] parameters = {

					new SqlParameter("@Tables", SqlDbType.NVarChar,512),

                    new SqlParameter("@PK", SqlDbType.NVarChar,128),

                    new SqlParameter("@Sort", SqlDbType.NVarChar,512),

                    new SqlParameter("@PageIndex", SqlDbType.Int),

                    new SqlParameter("@PageSize", SqlDbType.Int),

                    new SqlParameter("@Fields", SqlDbType.NVarChar,1024),

                    new SqlParameter("@Where", SqlDbType.NVarChar,1024),

                    new SqlParameter("@RecordCount", SqlDbType.Int)



			};

            parameters[0].Value = pi.TableName;

            parameters[1].Value = pi.Pk;

            parameters[2].Value = pi.Sort;

            parameters[3].Value = pi.PageIndex;

            parameters[4].Value = pi.PageSize;

            parameters[5].Value = pi.Fields;

            parameters[6].Value = pi.Fwhere;

            parameters[7].Direction = ParameterDirection.Output;



            DataTable data = DbHelperSQL.RunProcedure("UP_Paging", parameters, "ds").Tables[0];

            pi.Data = data;

            pi.RecordCount = Convert.ToInt32(parameters[7].Value);

            pi.PageCount = (long)Math.Ceiling(pi.RecordCount/(pi.PageSize+0.0));

        }


RPageInfo封装的分页信息,代码如下:

 

 

/// <summary>

    /// 分页信息类

    /// </summary>

    public class RPageInfo

    {



        private String _tableName;

        /// <summary>

        /// 表名,可以是子查询,但必须如下:

        ///     (select * from Dept) as model

        /// </summary>

        public String TableName

        {

            get { return _tableName; }

            set { _tableName = value; }

        }

        private String _pk=string.Empty;

        /// <summary>

        /// 主键或者其它字段

        /// </summary>

        public String Pk

        {

            get { return _pk; }

            set { _pk = value; }

        }

        private string _sort = string.Empty;

        /// <summary>

        /// 排序,例如:字段1 ASC,字段2 DESC

        /// 主键和排序字段是互斥的

        /// </summary>

        public string Sort

        {

            get { return _sort; }

            set { _sort = value; }

        }

        private string _fields="*";

        /// <summary>

        /// 要显示的字段,默认为*

        /// </summary>

        public string Fields

        {

            get { return _fields; }

            set { _fields = value; }

        }

        private string _fwhere = string.Empty;

        /// <summary>

        /// 筛选条件

        /// </summary>

        public string Fwhere

        {

            get { return _fwhere; }

            set { _fwhere = value; }

        }



        private int _pageIndex = 1;



        /// <summary>

        /// 页号

        /// </summary>

        public int PageIndex

        {

            get { return _pageIndex; }

            set { _pageIndex = value; }

        }

        private int _pageSize = 10;

        /// <summary>

        /// 页大小

        /// </summary>

        public int PageSize

        {

            get { return _pageSize; }

            set { _pageSize = value; }

        }



        private long _recordCount;

        /// <summary>

        /// 总条数

        /// </summary>

        public long RecordCount

        {

            get { return _recordCount; }

            set { _recordCount = value; }

        }

        private long _pageCount;

        /// <summary>

        /// 总页数

        /// </summary>

        public long PageCount

        {

            get { return _pageCount; }

            set { _pageCount = value; }

        }

        private DataTable _data;

        /// <summary>

        /// 结果集

        /// </summary>

        public DataTable Data

        {

            get { return _data; }

            set { _data = value; }

        }

    }


使用代码:

 

 

/// <summary>

        /// 获得医院信息

        /// </summary>

        /// <param name="pi"></param>

        /// <param name="isCityHospital">是否县级医院</param>

        /// <param name="hospitalName">医院名称</param>

        public void GetHospitals(RPageInfo pi,bool? isCityHospital,string hospitalName)

        {

            pi.TableName = "(SELECT * FROM [HOSPITAL] WHERE 1=1 ";

            if (isCityHospital!=null && isCityHospital.Value)

            {

                pi.TableName += " AND [LEVEL]=1";

            }

            else if (isCityHospital != null && !isCityHospital.Value)

            {

                pi.TableName += " AND [LEVEL]=0";

            }

            if (hospitalName != null && hospitalName.Trim() != string.Empty)

            {

                pi.TableName += " AND [HOSTPITALNAME] LIKE '%" + hospitalName + "%'";

            }

            pi.TableName += ") AS MODEL ";

            pi.Pk = "HospitalID";

            base.Pager(pi);

        }


 

 

你可能感兴趣的:(sqlserver)