国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 學(xué)院 > 開(kāi)發(fā)設(shè)計(jì) > 正文

從DAO轉(zhuǎn)換到ADO

2019-11-18 17:46:30
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友
SwitchfromDAOtoADO

BySamHuggill

Introduction

Afewdaysago,IstartedanewPRojectthathandlesalargedatabasecontainingHTMLcodeforacompletewebsite.Theprojecthastoallowthewebmastersofthewebsiteviewallupdatesmadetothesite,whentheyweremadeandbywhom.Theycanalsoeditthepagesonthesite,andautomaticallyuploadthem.

ThisprojectrequirestheuSEOfafairlylargedatabasethatneedstobeaccessedbymanypeoplefromdifferentPCs.IdecidedtouseSQLServerasthebackendtotheproject,butthismeantthatIcouldn注釋?zhuān)簍useDAOtoconnecttoit!Whatapain!

So,IdecideditwasabouttimeIstartedtolearnADO.ItookaquickglancearoundonthenetatmyusualVBsites,butfoundlittleornohelpformeonADO.

Well,asweprideourselveshereatVBSquareonaddingoriginalcontent,IdecidedIwouldwriteanarticleonusingADO.

ThisarticleisonlyreallytogetyoustartedonADO,andonlydiscussestheconnectionandrecordsetobjects.TherearemanymorefeaturesofADOthatyouwillneedtolookintobeforeyoutakeonaprojectusingADO.
Connectingtolocalandexternaldatabases

WithADO,youcanbuildallyourcodearoundalocaldatabaseandthen,veryeasilychangeonelineofcodethatwillallowyoutoaccessadatabaseonaSQLServer.

Thethingthattookmeawhiletofigureout,washowtoconnecttoadatabase.WithDAO,youusetheOpenDatabasecommandpassingthepathofthedatabaseasoneofthearguements.ButwithADO,youneedtobuildaconnectionstring.Toconnecttoalocaldatabase,usethefollowingconnectionstring:

ConnectionString="Provider=Microsoft.JET.OLEDB.3.51;DataSource=c:/mydb.mdb"

Thatmayseemabitcumbersome,butthisflexibilityprovidesyouwiththemeanstoconnecttoalmostanydatabaseinanyformatanywhere.ThefollowingconnectionstringisusedtoconnecttoaSQLSeverdatabasenamed注釋?zhuān)簆eople注釋?zhuān)?

ConnectionString="driver=[SQLServer];uid=admin;server=myserver;database=people"
SwitchfromDAOtoADO

BySamHuggill

UsingtheConnectionObject

TheConnectionobjectisthebasefromwhichalmostallADOfunctionsderivefrom.Youcanusethisobjecttocarryoutmostoftheactionsperformedinthesamplecode,usingSQLstatements.E.g.

mCN.Execute"DELETEFROMPeopleWHEREID=1"

Iwon注釋?zhuān)簍gointoanydetailaboutusingSQLstatements,buttheMSDNhassomeinfoonthem.

TheconnectionobjectreturnsarecordsetobjectifyouusetheExecutemehtod.YoucanusethistocreateaDLLanduseCOMtogetthecontentsofarecordset.e.g.

PublicSubGetRecordSet()AsADODB.Recordset
GetRecordSet=mCN.Execute("SELECT*FROMPeople")
EndSub

Thismeansthatyoucancentralizeallyoudatabasecodeintoonecomponent,preferablyaDLL.

UsingtheRecordsetObject

InADO,theRecordsetobjectisverysimilartotheDAORecordsetobject.Thismakesthingsaloteasierwhenportingyourcode,althoughyouwillneedtodeviseafewworkaroundstoovercomeafewmissingfeatures.

Forexample,whenyouinsertarecord,butneedtostoreitsID(AutoNumber)valueinthesameaction,youwouldnormallyusethiscodeinDAO:

Withrs
.AddNew
.Fields("Name").value=sNewValue
.Update
.Bookmark=.Lastmodified
m_intRcdID=.Fields("ID").value
.Close
EndWith
TheADORecordsetobjectdoesnotexposeaLastModifiedorLastUpdatedproperty,soweneedtousethefollowingworkaround:

Withrs
.AddNew
.Fields("Name").value=sNewValue
.Update
.Requery
.MoveLast
m_intRcdID=.Fields("ID").value
.Close
EndWith

Afterupdatingtherecordset(whichyoudon注釋?zhuān)簍needtodoifyouaremovingtoanotherrecord,asADOautomaticallyupdateschangesmadewhenyoumoverecords)youneedtorefreshtherecordsetusingtheRequerymethod.Thenyouneedtomovetothelastrecord,whichistheoneyouhavejustadded.Now,justextracttheIDvalueandstoreitinamembervariable.
Sampleapplication

TohelpyoumovefromDAOtoADO,IhavemadeasimilarsampleapplicationasIdidfortheBeginningDatabasesarticle.Thesampleoffersthesefeatures:

Addingnewrecords
Deletingrecords
Updatingrecords
Gettingrecorddata
Itisaverysimpledemo,butshouldhelpyoutounderstandthebasics.ItusethelatestversionofADO,version2.1.SeethesectionatthebottomfordownloadingtheADOLibrariesandthesampleapplcation.

Togetthesampleapplicationtowork,startanewStandardEXEProjectandaddareferencetotheMicrosoftActiveXDataObjects2.1Library(Project,References).Addfourcommandbuttons(cmdAdd,cmdDelete,cmdGet,cmdSave)andthreetextboxes(txtNotes,txtURL,txtName).Copy/pastethefollowingcodeintotheform:

OptionExplicit

注釋?zhuān)篜rivatereferencestotheADO2.1ObjectLibrary
PrivatemCNAsConnection
PrivatemRSAsNewRecordset

注釋?zhuān)篒nternalreferencetothecurrentrecordsIDvalue
PrivatemintRcdIDAsInteger

PrivateSubcmdAbout_Click()
frmAbout.ShowvbModal
EndSub

PrivateSubcmdAdd_Click()
AddRecord
EndSub

PrivateSubcmdClose_Click()
UnloadMe
EndSub

PrivateSubOpenConnection(strPathAsString)

注釋?zhuān)篊loseanopenconnection
IfNot(mCNIsNothing)Then
mCN.Close
SetmCN=Nothing
EndIf


注釋?zhuān)篊reateanewconnection
SetmCN=NewConnection

WithmCN
注釋?zhuān)篢oconnecttoaSQLServer,usethefollowingline:

注釋?zhuān)?ConnectionString="driver=[SQLServer];uid=admin;server=mysrv;database=site"

注釋?zhuān)篎orthisexample,wewillbeconnectingtoalocaldatabase
.ConnectionString="Provider=Microsoft.JET.OLEDB.3.51;DataSource="&strPath

.CursorLocation=adUseClient
.Open

EndWith

EndSub

PrivateSubAddRecord()


注釋?zhuān)篈ddanewrecordusingtherecordsetobject
注釋?zhuān)篊ouldbedoneusingtheconnectionobject
mRS.Open"SELECT*FROMPeople",mCN,adOpenKeyset,adLockOptimistic

WithmRS

.AddNew
.Fields("Name").Value=txtName.Text
.Fields("URL").Value=txtURL.Text
.Fields("Notes").Value=txtNotes.Text

注釋?zhuān)篈fterupdatingtherecordset,weneedtorefreshit,andthenmovetothe
注釋?zhuān)篹ndtogetthenewestrecord.Wecanthenretrievethenewrecord注釋?zhuān)簊id
.Update
.Requery
.MoveLast

mintRcdID=.Fields("ID").Value

.Close

EndWith

EndSub

PrivateSubDeleteRecord()

注釋?zhuān)篋eletearecordandclearthetextboxes

mRS.Open"SELECT*FROMPeopleWHEREID="&mintRcdID,mCN,adOpenKeyset,adLockOptimistic

mRS.Delete
mRS.Close

txtName.Text=""
txtURL.Text=""
txtNotes.Text=""

EndSub

PrivateSubGetInfo()

注釋?zhuān)篏etthedataforarecordbasedonitsIDvalue
mRS.Open"SELECT*FROMPeopleWHEREID="&
mintRcdID,mCN,adOpenKeyset,adLockOptimistic

WithmRS

txtName.Text=.Fields("Name").Value
txtURL.Text=.Fields("URL").Value
txtNotes.Text=.Fields("Notes").Value
.Close

EndWith

EndSub

PrivateSubUpdateRecord()

注釋?zhuān)篣pdatearecord注釋?zhuān)簊values
mRS.Open"SELECT*FROMPeopleWHEREID="&mintRcdID,mCN,adOpenKeyset,adLockOptimistic

WithmRS

.Fields("Name").Value=txtName.Text
.Fields("URL").Value=txtURL.Text
.Fields("Notes").Value=txtNotes.Text

.Update
.Close

EndWith

EndSub

PrivateSubcmdDelete_Click()
DeleteRecord
EndSub

PrivateSubcmdGet_Click()

注釋?zhuān)篈sktheuserwhichrecordshouldberetrievedandgetthedata
注釋?zhuān)篺orthatrecord
mintRcdID=Val(InputBox$("EnterIDofrecord:",App.Title,"1"))

GetInfo

EndSub

PrivateSubcmdSave_Click()
UpdateRecord
EndSub

PrivateSubForm_Load()

OpenConnectionApp.Path&"/people.mdb"

EndSub

PrivateSubForm_Unload(CancelAsInteger)

IfNot(mRSIsNothing)Then
SetmRS=Nothing
EndIf

IfNot(mCNIsNothing)Then
mCN.Close
SetmCN=Nothing
EndIf

EndSub->


發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 高青县| 天全县| 项城市| 巴林左旗| 龙井市| 武宣县| 深水埗区| 太湖县| 清镇市| 阳朔县| 柳州市| 台湾省| 滁州市| 苗栗市| 莱州市| 康马县| 东辽县| 平度市| 威信县| 三都| 远安县| 墨玉县| 宁武县| 枞阳县| 东兰县| 集贤县| 绿春县| 宝丰县| 开封县| 五寨县| 江门市| 浦江县| 延长县| 英吉沙县| 平山县| 西丰县| 新密市| 定陶县| 米泉市| 丽江市| 天镇县|