仓酷云

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

[学习教程] ASP网页设计ADO.NET:ADODataReader类

[复制链接]
海妖 该用户已被删除
跳转到指定楼层
楼主
发表于 2015-1-16 22:34:10 | 只看该作者 回帖奖励 |正序浏览 |阅读模式

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

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

x
使用cdonts,可以发送、查看邮件,实现webmail的功能。结合wsh,可以实现对nt主机的管理,如nt用户管理、iis虚拟主机设置、exchange邮箱设置等等,就像管理本地机一样方便。原码下载地点:
http://www.codeproject.com/dotnet/ADONET_datareader/ADONET_datareader.zip

Introduction
ADO.NETisthe.NETenhancedversionofADOthatweallknowandlove.ADO.NETaimstoaddresssomeofthedeficienciesoftraditionalADOsuchaslackoftypesafety,lackofanobjectorientedmodel,andinefficienciesinreturningrowsofdata.

Thisfirstarticlewilldemonstratethemostcommontaskwhenaccessingadatabase:queryingfordata,andtraversingthatdatafromstarttofinishinordertodisplaythecontents(orsubsetthereof)ofatable.

TheADODataReaderclass
ADO.NETreplacestheconceptofdatarowswiththeDataSetobject.Thisessentiallyprovidesuswithfullaccesstoagivendatabase,includingallrows,tablesandrelationshipsinanobjectorientedandtype-safemanner.Itis,however,totaloverkillforthesimplequeryandtraversalsthataremostoftenperformedondatabases.

Forthissimplecase.NETprovidesuswiththeADODataReaderclassthatisessentiallyatypesafereadonly,forwardonlyrowset.Allweneedtodoisopenaconnectiontoadatabase,sendanSQLcommand,thentraversethroughtheresultantADODataReaderusingtheReadcommandandprocesstheresults.

TheeasiestwaytoillustratethisistoshowyousomeC#code.ThiscodeopensanAccessdatabase,readsalltheinformationfromatable,thenpopulatesaListViewcontrolwiththedatainside.

Afewnotesonthecode:

StatusTextisaRichTextBoxcontroldeclaredasSystem.WinForms.RichTextBoxStatusText;
StatusText=newSystem.WinForms.RichTextBox();
listViewisalistviewcontroldeclaredasSystem.WinForms.ListViewlistView;
listView=newSystem.WinForms.ListView();
Thelistviewhasbeenplacedinreportmodewithgridlinesusing

listView.View=System.WinForms.View.Report;
listView.GridLines=true;


TheCode
ADOConnectionadoConnection=newADOConnection();

//TODO:Changethelocationofthisfile
//The@meansthatthestringwillbetreatedas-is,andthe
//swillnotbeinterpretedastheescapecharacter.
//Thissavestyping"D:ADONETdemo..."
StringstrDatabaseFile=@"D:ADONETdemoAuthors.mdb";

try
{
//Openaconnectiontothedatabase
adoConnection.ConnectionString="Provider=Microsoft.Jet.OLEDB.4.0;"+
"DataSource="+strDatabaseFile+";"+
"PersistSecurityInfo=False;";
adoConnection.Open();

//CreateanSQLcommand,setitsconnectionanditscommandtext
ADOCommandcommand=newADOCommand();
command.ActiveConnection=adoConnection;
command.CommandText="SELECT*FROMAuthor";

//Executethecommand,andreturntherowsinthedatareaderobject
ADODataReaderdataReader;
command.Execute(outdataReader);

//Getthenumberoffields(columns)inthetable
intnFields=dataReader.FieldCount;

//Setupthecolumnsinthelistviewusingthefieldsinthetable
listView.Clear();
for(inti=0;i<nFields;i++)
{
listView.InsertColumn(i,dataReader.GetName(i),100,HorizontalAlignment.Left);
}

//Filltherowsinthelistviewusingthedataintherows
intnRow=0;
while(dataReader.Read())
{
//Createanarrayofsubitemsforquickinsertion
//Thesubitemswillbeallfieldsintherowexceptfor
//thefirstfield
String[]subitems=newString[nFields-1];
for(inti=1;i<nFields;i++)
{
subitems[i-1]=dataReader.GetValue(i).ToString();
}

//Insertanewitemintothelistview,andaddthesubitems
//atthesametime.Theitemwillbethefirstfieldinthe
//row
listView.InsertItem(nRow,dataReader.GetValue(0).ToString(),
-1,subitems);
//nextrow.
nRow++;
}
dataReader.Close();

//Setthestatustext
StatusText.Text=nFields.ToString()+"columns,"+
nRow.ToString()+"rowsread";
}
catch
{
//Ifanerroroccuredalerttheuser
StatusText.Text="Erroroccurred";
}
finally
{
//Closetheconnectionifnecessary
if(adoConnection.State==DBObjectState.Open)
adoConnection.Close();
}

Thatsallthereistoit.Wehaveclosedthedatabaseconnectionbutsinceweareusingmanagedcodethereisnoneed(orway)todeletetheobjectsandmemoryweallocated.

AboutChrisMaunder
ChrisisthefounderandsiteadministratorforCodeProject.com.HesbeenprogramminginC/C++for10yearsandVisualC++/MFCfor4years.Hisbackgroundincludespureandappliedmathematics,engineeringandphysics,andheiscurrentlybasedinCanberra,Australia.
对于中小型web应用来说,php有很强的竞争力,linux+apache+mysql+php(lamp)的组合几乎可以胜任绝大多数网站的解决方案,对于大型应用来讲,对于系统架构要求更高,需要有成熟的框架支持,jsp的struts是个不错的框架,国内介绍它的资料也非常多,应用逐渐广泛起来。asp就不用说了,
愤怒的大鸟 该用户已被删除
9#
发表于 2015-3-25 09:37:25 | 只看该作者
ASP(ActiveServerPages)是Microsfot公司1996年11月推出的WEB应用程序开发技术,它既不是一种程序语言,也不是一种开发工具,而是一种技术框架,不须使用微软的产品就能编写它的代码,能产生和执行动态、交互式、高效率的站占服务器的应用程序。
只想知道 该用户已被删除
8#
发表于 2015-3-18 02:08:59 | 只看该作者
运用ASP可将VBscript、javascript等脚本语言嵌入到HTML中,便可快速完成网站的应用程序,无需编译,可在服务器端直接执行。容易编写,使用普通的文本编辑器编写,如记事本就可以完成。由脚本在服务器上而不是客户端运行,ASP所使用的脚本语言都在服务端上运行。
再现理想 该用户已被删除
7#
发表于 2015-3-11 09:03:02 | 只看该作者
ASP(ActiveServerPages)是Microsfot公司1996年11月推出的WEB应用程序开发技术,它既不是一种程序语言,也不是一种开发工具,而是一种技术框架,不须使用微软的产品就能编写它的代码,能产生和执行动态、交互式、高效率的站占服务器的应用程序。
活着的死人 该用户已被删除
6#
发表于 2015-3-3 02:48:23 | 只看该作者
ASP(ActiveServerPages)是Microsfot公司1996年11月推出的WEB应用程序开发技术,它既不是一种程序语言,也不是一种开发工具,而是一种技术框架,不须使用微软的产品就能编写它的代码,能产生和执行动态、交互式、高效率的站占服务器的应用程序。
蒙在股里 该用户已被删除
5#
发表于 2015-2-12 10:12:44 | 只看该作者
先学习用frontpage熟悉html编辑然后学习asp和vbscript建议买书进行系统学习
飘灵儿 该用户已被删除
地板
发表于 2015-2-5 15:08:24 | 只看该作者
学习是为了用的,是为了让你的程序产生价值,把握住这个原则会比较轻松点。除此之外,课外时间一定要多参加一些社会实践活动,来锻炼自己的能力。
乐观 该用户已被删除
板凳
发表于 2015-1-28 05:57:56 | 只看该作者
代码逻辑混乱,难于管理:由于ASP是脚本语言混合html编程,所以你很难看清代码的逻辑关系,并且随着程序的复杂性增加,使得代码的管理十分困难,甚至超出一个程序员所能达到的管理能力,从而造成出错或这样那样的问题。
小魔女 该用户已被删除
沙发
发表于 2015-1-19 16:46:16 | 只看该作者
最近在学asp,不要问我为什么不直接学.net,因为公司网站是asp做的所以有这个需要,卖了本书asp入门到精通,对里面的六大内置对象老是记不住,还有很多属性和方法看的头晕。
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2024-9-30 05:31

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

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