转至繁体中文版     | 网站首页 | 图文教程 | 资源下载 | 站长博客 | 图片素材 | 武汉seo | 武汉网站优化 | 
最新公告:     敏韬网|教学资源学习资料永久免费分享站!  [mintao  2008年9月2日]        
您现在的位置: 学习笔记 >> 图文教程 >> 站长学院 >> Web开发 >> 正文
asp.net(C#)海量数据表高效率分页算法(易懂,不使用存储过程)         ★★★★

asp.net(C#)海量数据表高效率分页算法(易懂,不使用存储过程)

作者:闵涛 文章来源:闵涛的学习笔记 点击数:1357 更新时间:2009/4/23 10:39:14

首先创建一张表(要求ID自动编号):
create table redheadedfile(
id int identity(1,1),
filenames nvarchar(20),
senduser nvarchar(20),
primary key(id)
)
然后我们写入50万条记录:
declare @i int
set @i=1
while @i<=500000
begin
    insert into redheadedfile(filenames,senduser) values(''''我的分页算法'''',''''陆俊铭'''')
    set @i=@i+1
end
GO
用Microsoft Visual Studio .NET 2003创建一张WebForm网页(本人起名webform8.aspx)
前台代码片段如下(webform8.aspx):
<%@ Page language="c#" Codebehind="WebForm8.aspx.cs" AutoEventWireup="false" Inherits="WebApplication6.WebForm8" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
 <HEAD>
  <title>WebForm8</title>
  <meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
  <meta content="C#" name="CODE_LANGUAGE">
  <meta content="JavaScript" name="vs_defaultClientScript">
  <meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">
 </HEAD>
 <body MS_POSITIONING="GridLayout">
  <form id="Form1" method="post" runat="server">
   <asp:datalist id="datalist1" AlternatingItemStyle-BackColor="#f3f3f3" Width="100%" CellSpacing="0"
    CellPadding="0" Runat="server">
    <ItemTemplate>
     <table width="100%" border="0" cellspacing="0" cellpadding="0">
      <tr>
       <td width="30%"

align="center"><%#DataBinder.Eval(Container.DataItem,"filenames")%></td>
       <td width="30%"

align="center"><%#DataBinder.Eval(Container.DataItem,"senduser")%></td>
       <td width="30%"

align="center"><%#DataBinder.Eval(Container.DataItem,"id")%></td>
      </tr>
     </table>
    </ItemTemplate>
   </asp:datalist>
   <div align="center">共<asp:label id="LPageCount" Runat="server" ForeColor="#ff0000"></asp:label>页/共

<asp:label id="LRecordCount" Runat="server" ForeColor="#ff0000"></asp:label>记录
    <asp:linkbutton id="Fistpage" Runat="server"

CommandName="0">首页</asp:linkbutton>&nbsp;&nbsp;&nbsp;&nbsp;<asp:linkbutton id="Prevpage" Runat="server" CommandName="prev">

上一页</asp:linkbutton>&nbsp;&nbsp;&nbsp;&nbsp;<asp:linkbutton id="Nextpage" Runat="server"

CommandName="next">下一页</asp:linkbutton>&nbsp;&nbsp;&nbsp;&nbsp;<asp:linkbutton id="Lastpage" Runat="server"

CommandName="last">尾页</asp:linkbutton>&nbsp;&nbsp;&nbsp;&nbsp;当前第<asp:label id="LCurrentPage" Runat="server"

ForeColor="#ff0000"></asp:label>页&nbsp;&nbsp;&nbsp;&nbsp;跳页<asp:TextBox ID="gotoPage" Runat="server" Width="30px"

MaxLength="5" AutoPostBack="True"></asp:TextBox></div>
  </form>
 </body>
</HTML>
后台代码片段如下(webform8.aspx.cs)
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Configuration;

namespace WebApplication6
{
 /// <summary>
 /// WebForm8 的摘要说明。
 /// </summary>
 public class WebForm8 : System.Web.UI.Page
 {
  protected System.Web.UI.WebControls.LinkButton Fistpage;
  protected System.Web.UI.WebControls.LinkButton Prevpage;
  protected System.Web.UI.WebControls.LinkButton Nextpage;
  protected System.Web.UI.WebControls.LinkButton Lastpage;
  protected System.Web.UI.WebControls.DataList datalist1;
  protected System.Web.UI.WebControls.DropDownList mydroplist;
  protected System.Web.UI.WebControls.Label LPageCount;
  protected System.Web.UI.WebControls.Label LRecordCount;
  protected System.Web.UI.WebControls.Label LCurrentPage;
  protected System.Web.UI.WebControls.TextBox gotoPage;
  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;//将运

[1] [2]  下一页


[C语言系列]NET 中C#的switch语句的语法  [系统软件]托拽Explore中的文件到VB.net的窗口
[系统软件]Boost库在XP+Visual C++.net中的安装  [常用软件]新配色面板:Paint.Net3.0RC1官方下载
[常用软件]用内建的“Net Meeting”聊天  [VB.NET程序]Henry的VB.NET之旅(三)—共享成员
[VB.NET程序]Henry的VB.NET之旅(二)—构造与析构  [VB.NET程序]Henry的VB.NET之旅(一)—失踪的窗体
[VB.NET程序]在托盘上显示Balloon Tooltip(VB.NET)  [VB.NET程序]Henry手记-VB.NET中动态加载Treeview节点(二)
教程录入:mintao    责任编辑:mintao 
  • 上一篇教程:

  • 下一篇教程:
  • 【字体: 】【发表评论】【加入收藏】【告诉好友】【打印此文】【关闭窗口
      注:本站部分文章源于互联网,版权归原作者所有!如有侵权,请原作者与本站联系,本站将立即删除! 本站文章除特别注明外均可转载,但需注明出处! [MinTao学以致用网]
      网友评论:(只显示最新10条。评论内容只代表网友观点,与本站立场无关!)

    同类栏目
    · Web开发  · 网页制作
    · 平面设计  · 网站运营
    · 网站推广  · 搜索优化
    · 建站心得  · 站长故事
    · 互联动态
    更多内容
    热门推荐 更多内容
  • 没有教程
  • 赞助链接
    更多内容
    闵涛博文 更多关于武汉SEO的内容
    500 - 内部服务器错误。

    500 - 内部服务器错误。

    您查找的资源存在问题,因而无法显示。

    | 设为首页 |加入收藏 | 联系站长 | 友情链接 | 版权申明 | 广告服务
    MinTao学以致用网

    Copyright @ 2007-2012 敏韬网(敏而好学,文韬武略--MinTao.Net)(学习笔记) Inc All Rights Reserved.
    闵涛 投放广告、内容合作请Q我! E_mail:admin@mintao.net(欢迎提供学习资源)

    站长:MinTao ICP备案号:鄂ICP备11006601号-18

    闵涛站盟:医药大全-武穴网A打造BCD……
    咸宁网络警察报警平台