海量(大量)数据GridView显示高效率分页实现方法

来源:互联网 发布:四川版小学英语软件 编辑:程序博客网 时间:2024/05/20 06:39
海量(大量)数据GridView显示高效率分页实现方法
2009-12-05 11:49

来源网络

问题:

      GridView显示海量数据时效率极其低,每次要从数据库从取出海量数据,这明显不现实。

解决方案:

       显示多少,就从数据库中读取多少数据,即每次取GridView显示的一个页面数据,点击下一页是,再取下一页相应的数据。

真的是很简单,但如果对asp.net不熟悉的人来说,还是有点难度。

就比如说:当页面刷新时,怎样使一个变量的值保持不变呢?很晕是吧

下面我们说分析实现方法

1、刷新页面,保持变量值不变

先讲怎样在刷新页面是使一个变量值保持不变,以前我都采用页面传值法,总觉得有点怪,本来就是本页的数据还要通过页面传值来保存。

后来在网上发现VewState这东东,有点像Session,用法基本上一样。

他们的区别是:Session是在页面之间,而ViewState是在它自己的页面保持一个值。

用法:

        ViewState["PageCounts"] = 3;

         下次要用时直接取出来就OK了。

 

2、GridView海量数据高效分页实现代码

 

下面代码来自互联网

 

   const int PageSize=20;//定义每页显示记录
   int PageCount,RecCount,CurrentPage,Pages,JumpPage;//定义几个保存分页参数变量

   private void Page_Load(object sender, System.EventArgs e)
   {
    if(!IsPostBack)
    {
     RecCount = Calc();//通过Calc()函数获取总记录数
     PageCount = RecCount/PageSize + OverPage();

   //计算总页数(加上OverPage()函数防止有余数造成显示数据不完整)

     ViewState["PageCounts"] = RecCount/PageSize - ModPage();

      //保存总页参数到ViewState(减去ModPage()函数防止SQL语句执行时溢出查询范围,可以用存储过程分页算法来理解这句)
     ViewState["PageIndex"] = 0;//保存一个为0的页面索引值到ViewState
     ViewState["JumpPages"] = PageCount;

        //保存PageCount到ViewState,跳页时判断用户输入数是否超出页码范围
     //显示LPageCount、LRecordCount的状态
     LPageCount.Text = PageCount.ToString();
     LRecordCount.Text = RecCount.ToString();
     //判断跳页文本框失效
     if(RecCount <= 20)
      gotoPage.Enabled = false;
     TDataBind();//调用数据绑定函数TDataBind()进行数据绑定运算
    }
   }
   //计算余页
   public int OverPage()
   {
    int pages = 0;
    if(RecCount%PageSize != 0)
     pages = 1;
    else
     pages = 0;
    return pages;
   }
      //计算余页,防止SQL语句执行时溢出查询范围
   public int ModPage()
   {
    int pages = 0;
    if(RecCount%PageSize == 0 && RecCount != 0)
     pages = 1;
    else
     pages = 0;
    return pages;
   }
        /*
   *计算总记录的静态函数
   *本人在这里使用静态函数的理由是:如果引用的是静态数据或静态函数,连接器会优化生成代码,去掉动态重定位项(对

海量数据表分页效果更明显)。
   *希望大家给予意见、如有不正确的地方望指正。
   */

   public static int Calc()
   {
    int RecordCount = 0;
    SqlCommand MyCmd = new SqlCommand("select count(*) as co from redheadedfile",MyCon());
    SqlDataReader dr = MyCmd.ExecuteReader();
    if(dr.Read())
     RecordCount = Int32.Parse(dr["co"].ToString());
    MyCmd.Connection.Close();
    return RecordCount;
   }
        //数据库连接语句(从Web.Config中获取)
   public static SqlConnection MyCon()
   {
    SqlConnection MyConnection = new SqlConnection(ConfigurationSettings.AppSettings["DSN"]);
    MyConnection.Open();
    return MyConnection;
   }
//对四个按钮(首页、上一页、下一页、尾页)返回的CommandName值进行操作
   private void Page_OnClick(object sender, CommandEventArgs e)
   {
    CurrentPage = (int)ViewState["PageIndex"];

   //从ViewState中读取页码值保存到CurrentPage变量中进行参数运算
    Pages = (int)ViewState["PageCounts"];//从ViewState中读取总页参数运算

    string cmd = e.CommandName;
    switch(cmd)//筛选CommandName
    {
     case "next":
      CurrentPage++;
      break;
     case "prev":
      CurrentPage--;
      break;
     case "last":
      CurrentPage = Pages;
      break;
     default:
      CurrentPage = 0;
      break;
    }
    ViewState["PageIndex"] = CurrentPage;

//将运算后的CurrentPage变量再次保存至ViewState

    TDataBind();//调用数据绑定函数TDataBind()
   }

   private void TDataBind()
   {
    CurrentPage = (int)ViewState["PageIndex"];

   //从ViewState中读取页码值保存到CurrentPage变量中进行按钮失效运算
    Pages = (int)ViewState["PageCounts"];

    //从ViewState中读取总页参数进行按钮失效运算
    //判断四个按钮(首页、上一页、下一页、尾页)状态
    if (CurrentPage + 1 > 1)
    {
     Fistpage.Enabled = true;
     Prevpage.Enabled = true;
    }
    else
    {
     Fistpage.Enabled = false;
     Prevpage.Enabled = false;
    }
    if (CurrentPage == Pages)
    {
     Nextpage.Enabled = false;
     Lastpage.Enabled = false;
    }
    else
    {
     Nextpage.Enabled = true;
     Lastpage.Enabled = true;
    }
            //数据绑定到DataList控件
    DataSet ds = new DataSet();
    //核心SQL语句,进行查询运算(决定了分页的效率:)
    SqlDataAdapter MyAdapter = new SqlDataAdapter("Select Top "+PageSize+" * from redheadedfile where id

not in(select top "+PageSize*CurrentPage+" id from redheadedfile order by id asc) order by id asc",MyCon());
    MyAdapter.Fill(ds,"news");
    datalist1.DataSource = ds.Tables["news"].DefaultView;
    datalist1.DataBind();
   //显示Label控件LCurrentPaget和文本框控件gotoPage状态
    LCurrentPage.Text = (CurrentPage+1).ToString();
    gotoPage.Text = (CurrentPage+1).ToString();
    //释放SqlDataAdapter
    MyAdapter.Dispose();
   }


      //跳页代码
   private void gotoPage_TextChanged(object sender, System.EventArgs e)
   {
    try
    {
     JumpPage = (int)ViewState["JumpPages"];

//从ViewState中读取可用页数值保存到JumpPage变量中

     //判断用户输入值是否超过可用页数范围值
     if(Int32.Parse(gotoPage.Text) > JumpPage || Int32.Parse(gotoPage.Text) <= 0)
     

Response.Write("<script>alert('页码范围越界!');location.href='WebForm8.aspx'</script>");
     else
     {
      int InputPage = Int32.Parse(gotoPage.Text.ToString()) - 1;

//转换用户输入值保存在int型InputPage变量中
      ViewState["PageIndex"] = InputPage;

//写入InputPage值到ViewState["PageIndex"]中
      TDataBind();

//调用数据绑定函数TDataBind()再次进行数据绑定运算
     }
    }
      //捕获由用户输入不正确数据类型时造成的异常
    catch(Exception exp)
    {
     Response.Write("<script>alert('"+exp.Message+"');location.href='WebForm8.aspx'</script>");
    }
   }

 

原创粉丝点击