分頁存儲過程-引

CREATE PROCEDURE [GetCustomersDataPage]

         @PageIndex INT,

         @PageSize  INT,

         @RecordCount INT OUT,

         @PageCount INT OUT

AS

SELECT @RecordCount = COUNT(*)  FROM   Customers

SET @PageCount = CEILING(@RecordCount * 1.0 / @PageSize)

DECLARE @SQLSTR NVARCHAR(1000)

IF @PageIndex = 0 OR @PageCount <= 1

         SET @SQLSTR =N'SELECT TOP '+STR( @PageSize )+

'  CustomerID, CompanyName,Address,Phone  FROM   Customers ORDER BY CustomerID DESC'

ELSE IF     @PageIndex = @PageCount - 1            

         SET @SQLSTR =N' SELECT * FROM ( SELECT TOP '+STR( @RecordCount - @PageSize * @PageIndex )+

'  CustomerID, CompanyName,Address,Phone  FROM   Customers ORDER BY CustomerID ASC ) TempTable  ORDER BY CustomerID DESC'

ELSE         

        SET @SQLSTR =N' SELECT TOP  '+STR( @PageSize )+' * FROM ( SELECT TOP '+STR( @RecordCount - @PageSize * @PageIndex )+

'  CustomerID, CompanyName,Address,Phone  FROM   Customers ORDER BY CustomerID ASC ) TempTable ORDER BY CustomerID DESC'

EXEC (@SQLSTR)

 

GO

 

select count(*) from Employee
--求的記錄數

SET @PageCount = CEILING(@RecordCount * 1.0 / @PageSize)
--求得頁數


select top 5 * from (select top (49-5*1) * from Employee order by ID ASC) tmptb  order by ID DESC

---2是pageIndex,頁索引,當前頁碼是 pageIndex+1.

 

獲取記錄數和頁數都採用存儲過程的輸出參數。

獲取數據源,這裏返回一個DataSet。

先定義了連個數據成員,

private int pageCount;//頁數

private int recordCount;//記錄數

//獲取單頁數據

private static DataSet GetCustomersData(int pageIndex,int pageSize,ref int recordCount,ref int pageCount)

{

     string connString = ConfigurationSettings.AppSettings["ConnString"];

     SqlConnection conn = new SqlConnection(connString);

     SqlCommand comm = new SqlCommand("GetCustomersDataPage",conn);

 

     comm.Parameters.Add(new SqlParameter("@PageIndex",SqlDbType.Int));

     comm.Parameters[0].Value = pageIndex;

 

     comm.Parameters.Add(new SqlParameter("@PageSize",SqlDbType.Int));

     comm.Parameters[1].Value = pageSize;

 

     comm.Parameters.Add(new SqlParameter("@RecordCount",SqlDbType.Int));

     comm.Parameters[2].Direction = ParameterDirection.Output;

 

     comm.Parameters.Add(new SqlParameter("@PageCount",SqlDbType.Int));

     comm.Parameters[3].Direction = ParameterDirection.Output;

 

     comm.CommandType = CommandType.StoredProcedure;

     SqlDataAdapter dataAdapter = new SqlDataAdapter(comm);

     DataSet ds = new DataSet();

     dataAdapter.Fill(ds);

 

     recordCount = (int)comm.Parameters[2].Value;

     pageCount = (int)comm.Parameters[3].Value;

 

     return ds;

}

//綁定數據到DataGrid,同時刷新數據總記錄數

private void DataGridDataBind()

{

     DataSet ds = GetCustomersData(PageIndex,PageSize,ref recordCount,ref pageCount);

     this.DataGrid1.VirtualItemCount = RecordCount;

     this.DataGrid1.DataSource = ds;

     this.DataGrid1.DataBind();

}

下面是分頁的幾個變量屬性

public int PageCount

{

     get{return this.DataGrid1.PageCount;}

}

 

public int PageSize

{

     get{return this.DataGrid1.PageSize;}

}

 

public int PageIndex

{

     get{return this.DataGrid1.CurrentPageIndex;}

     set{this.DataGrid1.CurrentPageIndex = value;}

}

 

public int RecordCount

{

     get{return recordCount;}

}

註冊DataGrid分頁事件

//分頁事件處理

private void DataGrid1_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)

{

     DataGrid dg = (DataGrid)source;

     dg.CurrentPageIndex = e.NewPageIndex;

     DataGridDataBind();

}

最好判斷當前頁面是否是第一次加載,防止重複加載兩次數據,

private void Page_Load(object sender, System.EventArgs e)

{

     if(!Page.IsPostBack)

     {

         DataGridDataBind();

     }

}

 

 

 

發佈了142 篇原創文章 · 獲贊 3 · 訪問量 15萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章