仓酷云

 找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 478|回复: 7
打印 上一主题 下一主题

[学习教程] ASP.NET教程之asp.net(C#)海量数据表高效力分页算法(...

[复制链接]
柔情似水 该用户已被删除
跳转到指定楼层
楼主
发表于 2015-1-16 22:36:29 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
前天傍晚我发表了《Java的跨平台就是一句谎言。》,原本就是周末闲来无事,发表一篇略带争议性的博文让大家都来吵吵架,发表自己的看法,根本就没想着谁把谁打倒,一个行业或者是技术阵营是无法用短期口水仗打到对手的。asp.net|存储历程|分页|数据|算法起首创立一张表(请求ID主动编号):
createtableredheadedfile(
idintidentity(1,1),
filenamesnvarchar(20),
sendusernvarchar(20),
primarykey(id)
)
然后我们写进50万笔记录:
declare@iint
set@i=1
while@i<=500000
begin
insertintoredheadedfile(filenames,senduser)values(我的分页算法,陆俊铭)
set@i=@i+1
end
GO
用MicrosoftVisualStudio.NET2003创立一张WebForm网页(自己起名webform8.aspx)
前台代码片断以下(webform8.aspx):
<%@Pagelanguage="c#"Codebehind="WebForm8.aspx.cs"AutoEventWireup="false"Inherits="WebApplication6.WebForm8"%>
<!DOCTYPEHTMLPUBLIC"-//W3C//DTDHTML4.0Transitional//EN">
<HTML>
<HEAD>
<title>WebForm8</title>
<metacontent="MicrosoftVisualStudio.NET7.1"name="GENERATOR">
<metacontent="C#"name="CODE_LANGUAGE">
<metacontent="JavaScript"name="vs_defaultClientScript">
<metacontent="http://schemas.microsoft.com/intellisense/ie5"name="vs_targetSchema">
</HEAD>
<bodyMS_POSITIONING="GridLayout">
<formid="Form1"method="post"runat="server">
<asp:datalistid="datalist1"AlternatingItemStyle-BackColor="#f3f3f3"Width="100%"CellSpacing="0"
CellPadding="0"Runat="server">
<ItemTemplate>
<tablewidth="100%"border="0"cellspacing="0"cellpadding="0">
<tr>
<tdwidth="30%"
align="center"><%#DataBinder.Eval(Container.DataItem,"filenames")%></td>
<tdwidth="30%"
align="center"><%#DataBinder.Eval(Container.DataItem,"senduser")%></td>
<tdwidth="30%"
align="center"><%#DataBinder.Eval(Container.DataItem,"id")%></td>
</tr>
</table>
</ItemTemplate>
</asp:datalist>
<divalign="center">共<asp:labelid="LPageCount"Runat="server"ForeColor="#ff0000"></asp:label>页/共
<asp:labelid="LRecordCount"Runat="server"ForeColor="#ff0000"></asp:label>纪录
<asp:linkbuttonid="Fistpage"Runat="server"
CommandName="0">首页</asp:linkbutton><asp:linkbuttonid="Prevpage"Runat="server"CommandName="prev">
上一页</asp:linkbutton><asp:linkbuttonid="Nextpage"Runat="server"
CommandName="next">下一页</asp:linkbutton><asp:linkbuttonid="Lastpage"Runat="server"
CommandName="last">尾页</asp:linkbutton>以后第<asp:labelid="LCurrentPage"Runat="server"
ForeColor="#ff0000"></asp:label>页跳页<asp:TextBoxID="gotoPage"Runat="server"Width="30px"
MaxLength="5"AutoPostBack="True"></asp:TextBox></div>
</form>
</body>
</HTML>
背景代码片断以下(webform8.aspx.cs)
usingSystem;
usingSystem.Collections;
usingSystem.ComponentModel;
usingSystem.Data;
usingSystem.Drawing;
usingSystem.Web;
usingSystem.Web.SessionState;
usingSystem.Web.UI;
usingSystem.Web.UI.WebControls;
usingSystem.Web.UI.HtmlControls;
usingSystem.Data.SqlClient;
usingSystem.Configuration;
namespaceWebApplication6
{
///<summary>
///WebForm8的择要申明。
///</summary>
publicclassWebForm8:System.Web.UI.Page
{
protectedSystem.Web.UI.WebControls.LinkButtonFistpage;
protectedSystem.Web.UI.WebControls.LinkButtonPrevpage;
protectedSystem.Web.UI.WebControls.LinkButtonNextpage;
protectedSystem.Web.UI.WebControls.LinkButtonLastpage;
protectedSystem.Web.UI.WebControls.DataListdatalist1;
protectedSystem.Web.UI.WebControls.DropDownListmydroplist;
protectedSystem.Web.UI.WebControls.LabelLPageCount;
protectedSystem.Web.UI.WebControls.LabelLRecordCount;
protectedSystem.Web.UI.WebControls.LabelLCurrentPage;
protectedSystem.Web.UI.WebControls.TextBoxgotoPage;
constintPageSize=20;//界说每页显现纪录
intPageCount,RecCount,CurrentPage,Pages,JumpPage;//界说几个保留分页参数变量

privatevoidPage_Load(objectsender,System.EventArgse)
{
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()举行数据绑定运算
}
}
//盘算余页
publicintOverPage()
{
intpages=0;
if(RecCount%PageSize!=0)
pages=1;
else
pages=0;
returnpages;
}
//盘算余页,避免SQL语句实行时溢出查询局限
publicintModPage()
{
intpages=0;
if(RecCount%PageSize==0&&RecCount!=0)
pages=1;
else
pages=0;
returnpages;
}
/*
*盘算总纪录的静态函数
*自己在这里利用静态函数的来由是:假如援用的是静态数据或静态函数,毗连器会优化天生代码,往失落静态重定位项(对
海量数据表分页效果更分明)。
*但愿人人赐与定见、若有不准确的中央看斧正。
*/
publicstaticintCalc()
{
intRecordCount=0;
SqlCommandMyCmd=newSqlCommand("selectcount(*)ascofromredheadedfile",MyCon());
SqlDataReaderdr=MyCmd.ExecuteReader();
if(dr.Read())
RecordCount=Int32.Parse(dr["co"].ToString());
MyCmd.Connection.Close();
returnRecordCount;
}
//数据库毗连语句(从Web.Config中猎取)
publicstaticSqlConnectionMyCon()
{
SqlConnectionMyConnection=newSqlConnection(ConfigurationSettings.AppSettings["DSN"]);
MyConnection.Open();
returnMyConnection;
}
//对四个按钮(首页、上一页、下一页、尾页)前往的CommandName值举行操纵
privatevoidPage_OnClick(objectsender,CommandEventArgse)
{
CurrentPage=(int)ViewState["PageIndex"];//从ViewState中读取页码值保留到CurrentPage变量中举行参数运

Pages=(int)ViewState["PageCounts"];//从ViewState中读取总页参数运算
stringcmd=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()
}
privatevoidTDataBind()
{
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控件
DataSetds=newDataSet();
//中心SQL语句,举行查询运算(决意了分页的效力:))
SqlDataAdapterMyAdapter=newSqlDataAdapter("SelectTop"+PageSize+"*fromredheadedfilewhereid
notin(selecttop"+PageSize*CurrentPage+"idfromredheadedfileorderbyidasc)orderbyidasc",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();
}
#regionWeb窗体计划器天生的代码
overrideprotectedvoidOnInit(EventArgse)
{
//
//CODEGEN:该挪用是ASP.NETWeb窗体计划器所必须的。
//
InitializeComponent();
base.OnInit(e);
}

///<summary>
///计划器撑持所需的办法-不要利用代码编纂器修正
///此办法的内容。
///</summary>
privatevoidInitializeComponent()
{
this.Fistpage.Command+=newSystem.Web.UI.WebControls.CommandEventHandler(this.Page_OnClick);
this.Prevpage.Command+=newSystem.Web.UI.WebControls.CommandEventHandler(this.Page_OnClick);
this.Nextpage.Command+=newSystem.Web.UI.WebControls.CommandEventHandler(this.Page_OnClick);
this.Lastpage.Command+=newSystem.Web.UI.WebControls.CommandEventHandler(this.Page_OnClick);
this.gotoPage.TextChanged+=newSystem.EventHandler(this.gotoPage_TextChanged);
this.Load+=newSystem.EventHandler(this.Page_Load);
}
#endregion
//跳页代码
privatevoidgotoPage_TextChanged(objectsender,System.EventArgse)
{
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
{
intInputPage=Int32.Parse(gotoPage.Text.ToString())-1;//转换用户输出值保留在int型
InputPage变量中
ViewState["PageIndex"]=InputPage;//写进InputPage值到ViewState["PageIndex"]中
TDataBind();//挪用数据绑定函数TDataBind()再次举行数据绑定运算
}
}
//捕捉由用户输出不准确数据范例时酿成的非常
catch(Exceptionexp)
{
Response.Write("<script>alert("+exp.Message+");location.href=WebForm8.aspx</script>");
}
}
}
}
人人来尝尝,效力是否是高了良多?
若有不当看人人来斧正
一个很大的类库。应用程序之所以难以跨平台,在于直接调用了特定平台的接口,而一个巨大的类库,就能极大地减少应用程序对平台的依赖。
深爱那片海 该用户已被删除
沙发
发表于 2015-1-19 18:23:32 | 只看该作者
众所周知,Windows以易用而出名,也因此占据不少的服务器市场。
飘灵儿 该用户已被删除
板凳
发表于 2015-2-4 23:30:39 | 只看该作者
可以通过在现有ASP应用程序中逐渐添加ASP.NET功能,随时增强ASP应用程序的功能。ASP.NET是一个已编译的、基于.NET的环境,可以用任何与.NET兼容的语言(包括VisualBasic.NET、C#和JScript.NET.)创作应用程序。另外,任何ASP.NET应用程序都可以使用整个.NETFramework。开发人员可以方便地获得这些技术的优点,其中包括托管的公共语言运行库环境、类型安全、继承等等。
透明 该用户已被删除
地板
发表于 2015-2-10 22:57:13 | 只看该作者
PHP的源代码完全公开,在OpenSource意识抬头的今天,它更是这方面的中流砥柱。不断地有新的函数库加入,以及不停地更新,使得PHP无论在UNIX或是Win32的平台上都可以有更多新的功能。它提供丰富的函数,使得在程式设计方面有着更好的资源。目前PHP的最新版本为4.1.1,它可以在Win32以及UNIX/Linux等几乎所有的平台上良好工作。PHP在4.0版后使用了全新的Zend引擎,其在最佳化之后的效率,比较传统CGI或者ASP等技术有了更好的表现。
冷月葬花魂 该用户已被删除
5#
发表于 2015-3-1 17:08:30 | 只看该作者
ASP.Net摆脱了以前ASP使用脚本语言来编程的缺点,理论上可以使用任何编程语言包括C++,VB,JS等等,当然,最合适的编程语言还是MS为.NetFrmaework专门推出的C(读csharp)。
admin 该用户已被删除
6#
发表于 2015-3-10 21:22:57 | 只看该作者
主流网站开发语言之JSP:JSP和Servlet要放在一起讲,是因为它们都是Sun公司的J2EE(Java2platformEnterpriseEdition)应用体系中的一部分。
莫相离 该用户已被删除
7#
发表于 2015-3-17 10:47:17 | 只看该作者
可以看作是VC和Java的混合体吧,尽管MS自己讲C#内核中更多的象VC,但实际上我还是认为它和Java更象一些吧。首先它是面向对象的编程语言,而不是一种脚本,所以它具有面向对象编程语言的一切特性。
第二个灵魂 该用户已被删除
8#
发表于 2015-3-24 07:44:58 | 只看该作者
我的意思是.net好用,从功能上来说比JAVA强还是很明显的。
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|仓酷云 鄂ICP备14007578号-2

GMT+8, 2024-5-16 08:05

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表