兰色精灵 发表于 2015-1-16 22:32:37

ASP网站制作之ADO+办理器功能

由于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一样简单方便,很快就能建一个站点,很多站长都是这样做的

金色的骷髅 发表于 2015-3-3 03:38:31

我们必须明确一个大方向,不要只是停留在因为学而去学,我们应有方向应有目标.

飘飘悠悠 发表于 2015-3-11 09:22:08

哪些内置对象是可以跳过的,或者哪些属性和方法是用不到的?

admin 发表于 2015-3-18 03:14:05

接下来就不能纸上谈兵了,最好的方法其实是实践。实践,只能算是让你掌握语言特性用的。而提倡做实际的Project也不是太好,因为你还没有熟练的能力去综合各种技术,这样只能使你自己越来越迷糊。

愤怒的大鸟 发表于 2015-3-25 10:39:00

交流是必要的,不管是生活还是学习我们都要试着去交流,通过交流我们可以学到很多我们自己本身所没有的知识,可以分享别人的经验甚至经历。
页: [1]
查看完整版本: ASP网站制作之ADO+办理器功效