仓酷云

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

[学习教程] ASP网站制作之ADO+办理器功效

[复制链接]
兰色精灵 该用户已被删除
跳转到指定楼层
楼主
发表于 2015-1-16 22:32:37 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式

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

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

x
由于ASP提供的是一对多的服务,所以用户的一些特殊需求很难得到满足。Inthepast,dataaccesswasdoneusingatwo-tiered,connectedmodel.Withtheincreaseddevelopmentof
multi-tieredapplications,theneedforadisconnectedmodelhasarisen.TheADO+managedprovidersgive
usthismodel.

ManagedprovidersareresponsibleforcreatingconnectionsbetweenDataSetobjectsanddatasourceslike
relationaldatabasesorXMLdocuments.Therearethreemainlevelstothemanagedproviderimplementation:

Connections,CommandsandParametersareresponsibleforcommunicationbetweenDataSetsanddatasources.
TheDataSetCommandactuallyretrievesthedataandprovidescolumnandtablemappings.
TheDataReaderprovideshigh-speed,forwardonlyaccesstodata.Underthecovers,theDataStreamobject
providesthedirectconnectiontothedatasource.
Lowerlevelobjectsconnecttothespecificdatasourcesandprovidethesystemspecificcommands.

AtthecenteroftheADO+modelaretheConnection,CommandandDataSetobjects.InthisarticleImgoing
tofocusontheConnectionandCommandobjects.YoucanreadmoreabouttheDataSetinmyprevious
article"ADO+DataSets,RecordsetsonSteroids?"

TwoWaystoConnect

Whytwomanagedproviders?MicrosofthasgivenusoneproviderforconnectingdirectlytoaSQLServer
databaseandoneforaccessingdataviaanOLEDBlayer.ThetwoConnectionobjectswithwhichtoconnect
todatastoresare:TheSQLConnectionforconnectingtoMicrosoftSQLServerandtheADOConnectionfor
connectingviaanOLEDBprovider.TheSQLmanagedprovidercanbeusedifyouincludetheSystem.Data.SQL
namespace.TousetheADOmanagedprovider,includetheSystem.Data.ADOnamespace.Aconnectioncanbe
establishedthefollowingtwoways(inC#):

SQL

StringsConnectionString="server=localhost;uid=sa;pwd=;database=pubs";
SQLConnectioncon=newSQLConnection(sConnectionString);
con.Open();


csharpindex.com/colorCode

ADO

StringsConnectionString="Provider=SQLOLEDB.1;
DataSource=localhost;
uid=sa;pwd=;InitialCatalog=pubs";

ADOConnectioncon=newADOConnection(sConnectionString);
con.Open();


csharpindex.com/colorCode

Thesetwomethodsofopeningaconnectiontoadatasourcelookremarkablysimilar,butletstakea
closerlook.TheconnectionstringfortheADOmanagedprovidershouldlookveryfamiliartoanyonewho
hasusedADO(itsidentical).TheSQLConnectionsupportsamultitudeofconnectionstringkeywords,but
themostcommononesareserver,uid,pwdanddatabase.Thefirstandlastareobvious.Thekeywordsuid
andpwdarejustshortenedversionsofthedatabaseuseridandpassword.

ExecuteAStatement

Inordertogetdatafromourdatasource,weneedtoexecutecommandsagainstthatdatasource.The
easiestwaytodothisisthrougheithertheADOorSQLCommandobjects.Likethis:

SQL

SQLCommandcmd=newSQLCommand(("SELECT*FROMAuthors",con);
SQLDataReaderdr=newSQLDataReader();
cmd.Execute(outdr);


csharpindex.com/colorCode

ADO

ADOCommandcmd=newADOCommand("SELECT*FROMAuthors",con);
ADODataReaderdr=newADODataReader();
cmd.Execute(outdr);


csharpindex.com/colorCode

Inordertogettothedata,weneedtoexecutethecommandandputthedataintoauseableobjectlike
theDataReader.ForamorecompletediscussionoftheDataReaderobjects,checkoutmyfirstarticleabout
dataaccesswiththeADO+DataReaderobject.

UsingStoredProcedures

Ok,sohowaboutsomethingalittlemorerealworld.Mostofususestoredprocedurestoaccessdatafrom
adatabase.Additionally,mostofthetimeweneedtopassparameterstothesestoredprocedures.Inthe
exampleabove,wegetbackalistofauthors.Letsassumewewanttoseeinformationaboutaspecific
author,weneedtodoacoupleofthings.Firstweneedtowriteasimplestoredprocedurethattakesone
parameter,anauthorid.Nextweneedtospecifyweareusingastoredprocedureandaddparameterstothe
parameterscollectionbeforeexecutingthecommand.Thestepsforbothprovidersareasfollows:

Createaparameter,specifyingtheparametername(ASITAPPEARSINTHESTOREDPROCEDURE),thedatatype
andthesizeoftheparameter.
Givetheparameteravalue
Addthenewparametertothecommandobjectsparameterscollection
Executethecommandasbefore.

SQL

SQLCommandcmd=newSQLCommand("spGetAuthorByID",con);
cmd.CommandType=CommandType.StoredProcedure;

SQLParameterprmID=newSQLParameter("@AuthID",
SQLDataType.VarChar,11);

prmID.Value="111-11-1111"
cmd.SelectCommand.Parameters.Add(prmID);
SQLDataReaderdr;
cmd.Execute(outdr);


csharpindex.com/colorCode

ADO

ADOCommandcmd=newADOCommand("spGetAuthorByID",con);
cmd.CommandType=CommandType.StoredProcedure;

ADOParameterprmID=newADOParameter("AuthID",
ADODataType.VarChar,11);

prmID.Value="111-11-1111";

cmd.SelectCommand.Parameters.Add(prmID);

ADODataReaderdr;
cmd.Execute(outdr);


csharpindex.com/colorCode

WhatsLeft?

Sowhatisleft?Plenty.EachoftheobjectsIhavediscussedherecouldbeelaboratedonfurther.Forthe
sakeofbrevity,IvetriedtostickwithwhatIthinkwillbeafairlytypicaluseofthemanaged
providers.Iveshownhowtomakeaconnectiontoadatasource,usecommandobjectsandspecifyasimple
parameter.Inmynextarticle,IwillbediscussingdatabindingandIwillalsoincludeadownloadable
workingexample!
因为现在数据库都使用标准的SQL语言对数据库进行管理,所以如果是标准SQL语言,两者基本上都可以通用的。SQLServer还有更多的扩展,可以用存储过程,数据库大小无极限限制。
深爱那片海 该用户已被删除
沙发
发表于 2015-1-19 16:23:33 | 只看该作者
学习ASP其实应该上升到如何学习程序设计这种境界,其实学习程序设计又是接受一种编程思想。比如ASP如何学习,你也许在以前的学习中碰到过。以下我仔细给你说几点:
蒙在股里 该用户已被删除
板凳
发表于 2015-1-28 06:07:12 来自手机 | 只看该作者
ASP.Net和ASP的最大区别在于编程思维的转换,而不仅仅在于功能的增强。ASP使用VBS/JS这样的脚本语言混合html来编程,而那些脚本语言属于弱类型、面向结构的编程语言,而非面向对象,这就明显产生以下几个问题:
莫相离 该用户已被删除
地板
发表于 2015-2-5 15:18:38 | 只看该作者
兴趣爱好,那么你无须学编程,申请一个域名和空间,在网上下载一些免费开源的CMS系统,你不用改代码,只须熟悉它们的后台操作,像office一样简单方便,很快就能建一个站点,很多站长都是这样做的
金色的骷髅 该用户已被删除
5#
发表于 2015-3-3 03:38:31 | 只看该作者
我们必须明确一个大方向,不要只是停留在因为学而去学,我们应有方向应有目标.
飘飘悠悠 该用户已被删除
6#
发表于 2015-3-11 09:22:08 | 只看该作者
哪些内置对象是可以跳过的,或者哪些属性和方法是用不到的?
admin 该用户已被删除
7#
发表于 2015-3-18 03:14:05 | 只看该作者
接下来就不能纸上谈兵了,最好的方法其实是实践。实践,只能算是让你掌握语言特性用的。而提倡做实际的Project也不是太好,因为你还没有熟练的能力去综合各种技术,这样只能使你自己越来越迷糊。
愤怒的大鸟 该用户已被删除
8#
发表于 2015-3-25 10:39:00 | 只看该作者
交流是必要的,不管是生活还是学习我们都要试着去交流,通过交流我们可以学到很多我们自己本身所没有的知识,可以分享别人的经验甚至经历。
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2024-9-29 21:30

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

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