實作最基本的高效分頁 需要兩個存儲過程,第一個是擷取全部資料數量的SQL,第二個是分頁用的SQL語句了
先上SQL代碼:分頁用的存儲過程
CREATE PROCEDURE Proc_GridView_Pager
@startIndex int,
@endIndex int
AS
BEGIN
select * from (
select a.id,a.saleName,a.price,a.count,a.amount,b.Name,a.inDate,row_number()over(order by a.inDate desc)rownum
from InBill a
inner join InBillType b
on a.InTypeId = b.id
) t
where t.rownum >=@startIndex and t.rownum<=@endIndex;
END
GO
擷取資料總數量的存儲過程
CREATE PROCEDURE Proc_GetAllData_Count
AS
selectcount(*) from inbill
END
GO
然後給aspnetPager添加PageChanged 事件代碼如下:這個是分頁用的代碼
string commandText = "Proc_GridView_Pager";
gvInBillList.DataSource = SQLHelper.GetDateSet(commandText, CommandType.StoredProcedure,
new SqlParameter("@startIndex", anpGridView.StartRecordIndex),
new SqlParameter("@endIndex", anpGridView.EndRecordIndex));
gvInBillList.DataBind();
最後一步了,就是上面提到的擷取全部資料的存儲過程,就是在這個時候用了,當頁面加載的時候調用方法
anpGridView.RecordCount = new InBillManager().GetAllDataCount();