真分页就是,只是从数据库提出相对应的记录。假分页,将数据库所有的数据调出,然后再分页。看一下真分页。
看一下存储过程。
USE [newssystem]
GO
/****** Object: StoredProcedure [dbo].[SelectPage] Script Date: 06/05/2017 17:30:01 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author: <Author,,Name>
-- Create date: <Create Date,,>
-- Description: <Description,,>
-- =============================================
ALTER PROCEDURE [dbo].[SelectPage]
@startpageindex int,
@endpageindex int
AS
BEGIN
--建立临时表
with temptb as (
select ROW_NUMBER() OVER(ORDER BY id desc) as 行号, * from news
)
select * from temptb where 行号 between @startpageindex and @endpageindex
END
D层
public DataTable selectpage(int start,int end)
{
DataTable dt = new DataTable();
string cmdText = "SelectPage";
SqlParameter[] paras = new SqlParameter[]{
new SqlParameter("@startpageindex",start),
new SqlParameter("@endpageindex",end)
};
dt = sqlhelper.ExecuteQuery(cmdText, paras, CommandType.StoredProcedure);
return dt;
}
B层
public DataTable selectpage(int start, int end)
{
return ndao.selectpage(start, end);
}
U层
if (!Page.IsPostBack)
{
DataTable dt = new NewsManager().SelectAll();
anp.RecordCount = dt.Rows.Count;
}
}
protected void anp_PageChanged(object sender, EventArgs e)
{
int start = anp.StartRecordIndex;
int end = anp.EndRecordIndex;
DataTable dt = new NewsManager().selectpage(start,end);
GridView1.DataSource = dt;
GridView1.DataBind();
}
本文介绍了一种数据库真分页的技术实现方式,通过存储过程在SQL Server中进行数据分页,避免了加载全部数据后再进行分页的问题,有效提高了大型数据集的查询效率。
1623

被折叠的 条评论
为什么被折叠?



