我们知道,在使用GridView进行分页的时候,通常是将所有的数据全部读取出来,然后再对其进行分页。可是,想过没有,如果有数以千万计的数据,一次这样全部读取然后绑定,每点击一次下一页又要这样绑定一次,或者修改数据后也是这样的重新绑定一次,你就会知道那速度有多慢了。这里,我采用的是存储过程进行分页,每一次只读取指定范围的数据,最终效果如图:
首先,我们要建立分页存储过程,打开Sqlver 2008,然后新建查询,这里我使用的数据库是Company,数据表是NewsInfoTable,
GO
CREAT procedure [dbo].[AutoPaging]
(
@pagesize int, --页面大小,如每页存储20条记录
@pageindex int --当前页码
)
as
set nocount on
begin
declare @indextable table(id int identity(1,1),nid int) --定义表变量
declare @PageLowerBound int --定义此页的底码
declare @PageUpperBound int --定义此页的顶码
set @PageLowerBound=(@pageindex-1)*@pagesize
set @PageUpperBound=@PageLowerBound+@pagesize
set rowcount @PageUpperBound
insert into @indextable(nid) select Id from NewsInfoTable
where AddedTime >dateadd(day,-365,getdate()) order by AddedTime desc
select O.id,O.Id,O.NewsTitle,O.NewsContents,(select NewsTypeName from NewsTypeTable as T where T.NewsType=O.NewsType) as TypeName,O.AddedTime,O.ReadedAcounts from NewsInfoTable O,@indextable t
where O.Id=t.nid and t.id>@PageLowerBound
and t.id<=@PageUpperBound order by t.id
end
set nocount off
建立好存储过程之后,我们就可以写一个C#方法来获取指定页的数据了,
SqlHelper sqlHelper=new SqlHelper();
/// <summary>
/// 获取分页数据
/// </summary>
/// <param name="size">页面大小</param>
/// <param name="currentPage">当前页码</param>
/// <returns></returns>
public DataSet GetPageData(int size, int currentPage)
{
string procName = "AutoPaging";
SqlParameter pramSize = sqlHelper.MakeInParam("@pagesize", SqlDbType.Int, 50, size);
SqlParameter pramPageIndex= sqlHelper.MakeInParam("@pageindex", SqlDbType.Int, int.MaxValue, currentPage);
SqlParameter[] prams = new SqlParameter[2] { pramSize, pramPageIndex };
return sqlHelper.RunProcReturn(procName,prams,"NewsPageTb");
}
public class SqlHelper
{
/// <summary>
/// 传入参数并转换为SqlParameter类型
/// </summary>
/// <param name="paramName">存储过程名或命令文本</param>
/// <param name="dbType">参数类型</param>
/// <param name="size">参数大小</param>
/// <param name="value">参数值</param>
/// <returns>新的Parameter对象</returns>
public SqlParameter MakeInParam(string paramName, SqlDbType dbType, int size, object value)
{
return MakeParam(paramName, dbType, size, ParameterDirection.Input, value);
}
/// <summary>
/// 初始化参数值
/// </summary>
/// <param name="paramName">存储过程名或命令文本</param>
/// <param name="dbType">参数类型</param>
/// <param name="size">参数大小</param>
/// <param name="direction">参数方向</param>
/// <param name="value">参数值</param>
/// <returns></returns>
public SqlParameter MakeParam(string paramName, SqlDbType dbType, int size, ParameterDirection direction, object value)
{
SqlParameter param;
if (size > 0)
{
param = new SqlParameter(paramName, dbType, size);
}
else
{
param = new SqlParameter(paramName, dbType);
}
param.Direction = direction;
if (!(direction == ParameterDirection.Output && value == null))
{
param.Value = value;
}
return param;
}
/// <summary>
/// 执行存储过程,并返回Dataset值
/// </summary>
/// <param name="procName">存储过程名</param>
/// <param name="tbName">数据表名称</param>
/// <returns></returns>
public DataSet RunProcReturn(string procName, SqlParameter[] prams, string tbName)
{
SqlDataAdapter adapter = CreateDataAdapter(procName, prams, CommandType.StoredProcedure);
DataSet ds = new DataSet();
adapter.Fill(ds, tbName);
this.Close();
return ds;
}
}
然后,让页面一加载的时候,绑定指定数据的数据,点击下一页,再获取指定范围的数据,这样,真正的分页查询就实现了。