ObjectDataSource与GridView结合,实现最快速的分页

  前几天做网站的后台程序,我用的objectDataSource做为数据源,用GridView控件来实现绑定数据!做到GridView的高效分页问题遇到点小麻烦,经过两天的查询资料,今天终于把问题解决了!写这篇博文的目的就是来分享GridView高效分页的方法,我尽量讲的细一点,让大家都能明白!

  GridView控件绑定的数据来源于objectDataSource数据源,而数据源直接调用的是业务层的方法。因此要做到高效分页,必须来控制数据源控件,一页如果显示10条数据,那么数据源控件就直接向业务层要这10条数据。那么数据源控件又是通过什么东西来告诉业务层说我就要这10条数据,下面我们来看看数据源控件objectDataSource的一些属性: 

     

首先我们得设置这5个属性

  1. EnablePaging设置为True启用分页
  2. MaximumRowsParameterName="PageSize"这个是设置每页有多少条数据的参数,待会直接会把值传给业务层的方法!
  3. SelectCountMethod="GetPageCount"指定业务层中查询一共有多少条数据的方法,要在业务层写出此方法
  4. SelectMethod="GetPageDataByobj"指定业务层中查询数据的方法,要在业务层写出此方法,并且接受MaximumRowsParameterName,StartRowIndexParameterName两个参数
  5. StartRowIndexParameterName="lastRowIndex"这个是上一页最后一条数据的索引下标,可能听着有点迷糊,这个是什么东东。我来详细解释一下,假设这是第一页的10条数据

如果MaximumRowsParameterName=10,

要显示第一页的数据那么StartRowIndexParameterName=0

第二页的数据是StartRowIndexParameterName=10

第三页的数据是StartRowIndexParameterName=20

如果要根据StartRowIndexParameterName和MaximumRowsParameterName要在数据库中查询某页数据时,我们可以这样写:

select * from
(
    select row_number() over(order by Id) as rowNum,* from Books
) t
where t.rowNum>@lastRowIndex and t.rowNum<=@lastRowIndex+@PageSize

现在写个分页存储:

create proc GetPagebyObjData
@PageSize int,--页容量
@lastRowIndex int--上一页最后一行的下标
as

begin
select * from
(
    select row_number() over(order by Id) as rowNum,* from Books
) t
where t.rowNum>@lastRowIndex and t.rowNum<=@lastRowIndex+@PageSize

end

数据库的存储过程写好了,下面我们来写业务逻辑层和数据访问层的方法

DAL层:

  /// <summary>
        /// GridView的分页
        /// </summary>
        /// <param name="PageSize">页容量</param>
        /// <param name="lastRowIndex">上一页最后一行的下标</param>
        /// <returns></returns>
        public DataTable GetPageDataByobj(int PageSize,int lastRowIndex)
        {
            DataTable dt = new DataTable();
            using(SqlConnection conn=new SqlConnection(connectionString))
            {
                conn.Open();
                //调用存储过程GetPagebyObjData
                SqlDataAdapter adapter = new SqlDataAdapter("GetPagebyObjData", conn);
                SqlParameter[] paras = {new SqlParameter("@PageSize",PageSize),
                                       new SqlParameter("@lastRowIndex",lastRowIndex)};
                //讲参数集合添加到查询命令中去
                adapter.SelectCommand.Parameters.AddRange(paras);
                //设置 查询命令类型 为 存储过程
                adapter.SelectCommand.CommandType = CommandType.StoredProcedure;

                adapter.Fill(dt);
                return dt;

            }
 
        }

        /// <summary>
        /// 获得所有数据数量
        /// </summary>
        /// <returns></returns>
        public int GetPageCount()
        {
            int i = 0;
            string sqlCommdText = "select count(Id) from Books";
            i = Convert.ToInt32(DbHelperSQL.GetSingle(sqlCommdText));
            return i;
        }

BLL层:

//此方法objectDataSource调用
        public List<Model.Book> GetPageDataByobj(int PageSize, int lastRowIndex)
        {
            //把DataTable类型转换为泛型
            return DataTableToList(dal.GetPageDataByobj(PageSize,lastRowIndex));
        }

        public int GetPageCount()
        {
            return dal.GetPageCount();
        }

最后还有还有一步就是把objectDataSource自动生成的这两个参数得干掉:

   小弟第一次发表技术博客,可能有些地方有缺漏,还望各位不吝赐教!

posted @ 2012-09-24 22:21  大麦种子  阅读(560)  评论(0编辑  收藏  举报
4