abaseisappropriatelyconfigured.
Architecture
ADO.NETreliesondataproviderstoprovideaccesstotheunderlyingdatasource.Eachdataproviderexposesasetofobjectsthatyouusetomanageconnections,retrievedata,andupdatedata.Thecoreobjectsarethefollowing:
*Connection
*Command
*DataReader
*DataAdapter
Inaddition,ADO.NETprovidestheDataSetobject,whichprovidesadisconnectedcacheofdata.TheDataSetobjectdoesnotrequireaspecifictypeofdatasourceandisnottiedtotheunderlyingdatasourcethatthedatawasobtainedfrom.
ThebasicADO.NETarchitectureisshowninFigure12.1.
Figure12.1:ADO.NETarchitecture
ThefollowinglistoutlinesthepurposeofeachofthemainADO.NETobjects:
*Connection.Thisobjectrepresentsaconnectiontoadatabase.
*Command.ThisobjectrepresentsanSQLstatementthatisrunwhileconnectedtoadatasource.ThisobjectcanbeastoredprocedureoradirectSQLstatement.
*DataReader.Thisobjectretrievesaread-only,forward-onlystreamofdatafromadatabase.TheDataReaderobjectisdesignedforconnectedscenariosandoffersbetterperformancethanreadingdataintoaDataSetobjectattheexpenseoffunctionality.FormoreinformationabouthowtouseDataReaderobjectsandDataSetobjects,see"DataSetvs.DataReader"laterinthischapter.
*DataAdapter.ThisobjectchannelsdatatoandfromaDataSetobjectandtheunderlyingdatasource.TheDataAdapterobjectalsoprovidesenhancedbatchupdatefeaturesthatwerepreviouslyassociatedwiththeADORecordsetobject.
*DataSet.TheDataSetobjectrepresentsadisconnected,cachedsetofdata.TheDataSetisindependentoftheproviderandisnottiedtotheunderlyingdatasourcethatmighthavebeenusedtopopulateit.DataSetcaneasilybepassedfromcomponenttocomponentthroughthevariouslayersofanapplication,anditcanbeserializedasXML.
YoushouldbeawareofthewayaDataSetisinternallyconstructedbecausetheDataSetcontainsapotentiallylargenumberofinternalobjects.ThismeansthatalargenumberofmemoryallocationsarerequiredtoconstructatypicalDataSet.
ADataSetconsistsofoneormoreDataTableobjectstogetherwithDataRelationobjectsthatmaintaintablerelationshipinformation.EachDataTablecontainsDataRowobjectsandDataColumnobjects.ConstraintobjectsareusedtorepresentaconstraintthatcanbeenforcedononeormoreDataColumnobjects.
NoteYoucanalsousetypeddatasetsthatderivefromthebasicDataSetclass.Typeddatasetsprovidebenefitsatbuildtimeandatruntime.Formoreinformation,see"TypedDataSets"laterinthischapter.
*DataView.AlthoughtheDataViewobjectisnotshowninFigure12.1,youcanuseaDataViewtosortandfilterdatainaDataTable.Thiscapabilityisoftenusedfordatabinding.
AbstractingDataAccess
ADO.NETisdesignedaroundasetofgenericinterfacesthatabstracttheunderlyingdataprocessingfunctionality.Youcanusetheseinterfacesdirectlytoabstractyourdataaccesslayersothatyoucanminimizetheimpactofchangingthetypeofdatasourcethatyouuse.Abstractingdataaccessisextremelyhelpfulwhenyouaredesigningsystemswhereyourcustomerchoosesthedatabaseserver.
ThecoreinterfacesprovidedbyADO.NETarefoundintheSystem.Datanamespace:
*IDbConnection.Thisisaninterfaceformanagingdatabaseconnections.
*IDbCommand.ThisisaninterfaceforrunningSQLcommands.
*IDbTransaction.Thisisaninterfaceformanagingtransactions.
*IDataReader.Thisisaninterfaceforreadingdatareturnedbyacommand.
*IDataAdapter.Thisisaninterfaceforchannelingdatatoandfromdatasets.
Thevariousproviderobjects,suchasSqlConnectionandOleDbConnection,implementthesegenericADO.NETdataaccessinterfaces.Ifyoudecidetoprogramagainstthegenericinterfaces,beawareofthefollowingissues:
*Thereissomesmallcostassociatedwithavirtualcallthroughaninterface.
*Certainexpandedfunctionalityislostwhenyouusethegenericinterfaces.Forexample,theExecuteXmlReadermethodisimplementedbytheSqlCommandobjectbutnotbytheIDbCommandinterface.
*Thereisnogenericbaseexceptiontype,soyoumustcatchprovider-specificexceptiontypes,suchasSqlException,OleDbException,orOdbcException.
*Whenyouusethegenericinterfaces,youcannottakeadvantageofdatabase-specifictypesthataredefinedforthemanagedproviders;forexample,youcannottakeadvantageofSqlDbTypeinSqlClientandOracle-specifictypesintheOracleprovider.Usingspecificdatabasetypesishelpfulfortypecheckingandparameterbinding.
PerformanceandScalabilityIssues
Thefollowingisalistofthemainissuesthatcanadverselyaffecttheperformanceandscalabilityofdataaccessinyourapplication.
*Inefficientqueries.Queriesthatprocessandthenreturnmorecolumnsorrowsthannecessarywasteprocessingcyclesthatcouldbestbeusedforservicingotherrequests.Queriesthatdonottakeadvantageofindexesmayalsocausepoorperformance.
*Retrievingtoomuchdata.Toomuchdatainyourresultsisusuallytheresultofinefficientqueries.TheSELECT*queryoftencausesthisproblem.Youdonotusuallyneedtoreturnallthecolumnsinarow.Also,analyzetheWHEREclauseinyourqueriestoensurethatyouarenotreturningtoomanyrows.TrytomaketheWHEREclauseasspecificaspossibletoensurethattheleastnumberofrowsarereturned.
*Inefficientormissingindexes.Queryefficiencydecreaseswhenindexesaremissingbecauseafulltablescanmustbeperformed.Also,asyourdatagrows,tablesmaybecomefragmented.Failuretoperiodicallyrebuildindexesmayalsoresultinpoorqueryperformance.
*Unnecessaryroundtrips.Roundtripssignificantlyaffectperformance.Theyaresubjecttonetworklatencyandtodownstreamserverlatency.Manydata-drivenWebsitesheavilyaccessthedatabaseforeveryuserrequest.Whileconnectionpoolinghelps,theincreasednetworktrafficandprocessingloadonthedatabaseservercanadverselyaffectperformance.Keeproundtripstoanabsoluteminimum.
*Toomanyopenconnections.Connectionsareanexpensiveandscarceresource,whichshouldbesharedbetweencallersbyusingconnectionpooling.Openingaconnectionforeachcallerlimitsscalability.Toensuretheefficientuseofconnectionpooling,avoidkeepingconnectionsopenandavoidvaryingconnectionstrings.
*Failuretoreleaseresources.Failingtoreleaseresourcescanpreventthemfrombeingreusedefficiently.Ifyoufailtocloseconnectionsbeforetheconnectionsfalloutofscope,theyarenotreclaimeduntilgarbagecollectionoccursfortheconnection.Failingtoreleaseresourcescancauseseriousresourcepressureandleadtoshortagesan
上一篇:
vb企业工资管理系统(论文和程序)
下一篇:
[新闻文章]勇者天空文章主页v1_065567news10