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

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章