GridView用储存过程做分页的完整C#代码

using System;
using System.Collections.Generic;
using System.Text;
using DataBase;
using System.Data;
using System.Data.SqlClient;

namespace WebDal
{
    /// <summary>
    /// 分页类
    /// </summary>
    public class Pagination
    {
        #region 字段
        private int _PageSize = 12;//每页记录数       
        private string _TableName="";//表名
        private int _TableSize=0;//总记录数
        private int _Pages=0;//总页数
        private int _Page=1;//当前页
        private Taxis Tax=Taxis.ASC;
        private string _OrderBy ="desc";//排序方式
        private string _FldName="ID";//排序字段 
        private string _Condition="";//查询条件
        #endregion

 

        #region 属性
        /// <summary>
        /// 设置每页记录数
        /// </summary>
        public int PageSize
        {
            set
            {
                _PageSize = value;
                init();               
            }
            get { return _PageSize; }
        }
        /// <summary>
        /// 设置分页表名称
        /// </summary>
        public string TableName
        {
            set
            {
                _TableName = value;
                init();
            }
            get { return _TableName; }
        }
        /// <summary>
        /// 总记录数
        /// </summary>
        public int DataSisz
        {          
            get { return _TableSize; }
        }
        /// <summary>
        /// 总页数
        /// </summary>
        public int Pages
        {
            get { return _Pages; }
        }
        /// <summary>
        /// 设置排序方式
        /// </summary>
        public Taxis Orderby
        {
            set
            {
               // _OrderBy = value;
                Tax = value;
                if (Tax == Taxis.DESC)
                {
                    _OrderBy = "DESC";
                }
                else if (Tax == Taxis.ASC)
                {
                    _OrderBy = "ASC";
                }
            }
        }
        /// <summary>
        /// 设置排序字段
        /// </summary>
        public string FldName
        {
            set { _FldName = value; }
        }
        /// <summary>
        /// 设置或获取当前页
        /// </summary>
        public int NowPage
        {
            set { _Page = value; }
            get { return _Page; }
        }
        /// <summary>
        /// 设置查询条件
        /// </summary>
        public string Condition
        {
            set
            {
                _Condition = value;
                init();
            }
        }
        #endregion


    
        #region 构造
        /// <summary>
        /// 带1个参数的构造函数
        /// </summary>
        /// <param name="TableName">需查询表名</param>
        public Pagination(string TableName)
        {
            init();
        }
        /// <summary>
        /// 带2个参数的构造函数
        /// </summary>
        /// <param name="TableName">需查询表名</param>
        /// <param name="PageSize">每页的记录数</param>
        public Pagination()
        {
            ///构造
        }
        #endregion

 

        #region 方法
        /// <summary>
        /// 初始化
        /// </summary>
        /// <param name="TableName">表名</param>
        /// <param name="PageSize">每页记录数</param>
        private void init()
        {
            //_TableName = TableName;
            //_PageSize = PageSize;
            string strSql;
            if(_Condition=="")
                strSql="select count(*) from " + _TableName;
            else
                strSql="select count(*) from " + _TableName+" where "+_Condition;
            DataSet ds = SqlDataBase.ExecDataSet(strSql);
            string aa = ds.Tables[0].Rows[0][0].ToString();
            _TableSize =Convert.ToInt32(aa);
            if (_TableSize / PageSize < 1)
            {
                _Pages = 1;
            }
            else
            {
                if (_TableSize % PageSize == 0)
                {
                    _Pages = _TableSize / PageSize;
                }
                else
                {
                    _Pages = _TableSize / PageSize + 1;
                }
            }

        }
               
        public DataSet ExecPage()
        {
            SqlParameter []param ={ new SqlParameter("@TableName", SqlDbType.NVarChar),
                                  new SqlParameter("@PageSize", SqlDbType.Int),
                                  new SqlParameter("@Page", SqlDbType.Int),
                                  new SqlParameter("@OrderBy",SqlDbType.NVarChar),                                 
                                  new SqlParameter("@FldName",SqlDbType.NVarChar),                                 
                                  new SqlParameter("@Condition",SqlDbType.NVarChar)};
            param[0].Value = _TableName;
            param[1].Value = _PageSize;
            param[2].Value = _Page;
            param[3].Value = _OrderBy;
            param[4].Value = _FldName;
            param[5].Value = _Condition;
            DataSet ds= SqlDataBase.ExecDataSet("SP_Page",param);
            return ds;
        }
        #endregion
    }

 

    /// <summary>
    /// sql枚举排序方式
    /// </summary>
    public enum Taxis
    {
        ASC = 0,
        DESC = 1
    }
}

 

 

 


SQL code

CREATE   PROCEDURE [dbo].[SP_Page]
    @TableName nvarchar(50),--表名
    @pagesize int, --每页记录数
    @page int, --指定页
    @OrderBy Nvarchar(10)='desc',--排序方式
    --@pages int OUTPUT,--总页数
    @Annal int=null,
    @FldName nvarchar(50),--要排序字段
    @Condition nvarchar(500),--查询条件
    @tmpCondition nvarchar(500)=''
AS
BEGIN

    if @Condition<>''
    BEGIN
        set @tmpCondition=' where '+@Condition
        set @Condition=@Condition+' and'
    END
   
   
    set @Annal=@pagesize*(@page-1)

set @tmpCondition='select top '+cast(@pagesize as varchar(20))+' * from '+@TableName+
          ' where '+ @Condition+' ('+@fldName+' not in (select top '+
          cast(@Annal as varchar(20)) +' '+@fldName+' from '+@TableName+@tmpCondition+
          ' order by '+@fldName+' '+@OrderBy+')) order by '+@fldName+' '+@OrderBy
exec(@tmpCondition)
END

GO

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值