在當(dāng)今提出的多種動(dòng)態(tài)網(wǎng)頁(yè)(Dhtml)解決方案中都強(qiáng)調(diào)了與數(shù)據(jù)庫(kù)的連接,其實(shí)網(wǎng)頁(yè)接掛后臺(tái)數(shù)據(jù)庫(kù)也是當(dāng)前的熱門應(yīng)用,在電子商務(wù)等領(lǐng)域有著廣泛的應(yīng)用.MicroSoft為了適應(yīng)其發(fā)展推出了新的ADO(Active Data Object)模型,通過ODBC的連接可以對(duì)當(dāng)前流行的桌面數(shù)據(jù)庫(kù)系統(tǒng)提供方便,快潔的連接.在這里,我想通過ADO在ASP技術(shù)中的應(yīng)用來總結(jié)一下ADO對(duì)象以及其各自的屬性和方法.
一、ADO對(duì)象模型:
下面簡(jiǎn)單介紹一下ADO的對(duì)象模型
ADO有三大對(duì)象,即Connection對(duì)象,Command對(duì)象,RecordSet對(duì)象.
Connection(對(duì)象)
─Errors(集合)─Error(對(duì)象)
Command(對(duì)象)
─Parameters(集合)─Parameter(對(duì)象)
RecordSet(對(duì)象)
─Fields(集合)─Field(對(duì)象)
其中Connection對(duì)象,Command對(duì)象,RecordSet對(duì)象和Field對(duì)象又分別具有Properities集合而產(chǎn)生Properity對(duì)象.我們?cè)赼sp編程中已經(jīng)對(duì)這幾個(gè)對(duì)象有了足夠的了解.下面是以上三大對(duì)象的相互關(guān)系.
Command.ActiveConnection->Connection
RecordSet.ActiveConnection->Connection
Connection.Execute->RecordSet
Command.Execute->RecordSet
RecordSet.Source->Command
好了,有了以上的關(guān)系表后,你應(yīng)該能理解有一些等價(jià)的表達(dá)形式了.
形式1
set rs = Server.CreateObject("ADODB.RecordSet")
strConn = "driver={SQL Server};server=srv;"& _
"uid=sa;pwd=;database=pubs"
strSQL = "Select * From Employee ;"
rs.open strSQL,strConn,,,adCmdText
形式2
set conn = Server.CreateObject("ADODB.Connection")
strConn ="driver={SQL Server};server=srv;"& _
"uid=sa;pwd=;database=pubs"
conn.open strConn
set rs = Server.CreateObject("ADODB.RecordSet")
rs.ActiveConnection=conn(注意此句)
strSQL = "Select * From Employee ;"
rs.open strSQL,,,,adCmdText
"上面這句也可以寫成rs.open strSQL,conn,,,adCmdText
形式3
set conn = Server.CrreateObject("ADODB.Connection")
set rs = Server.CreateObject("ADODB.RecordSet")
strConn ="driver={SQL Server};server=srv;"& _
"uid=sa;pwd=;database=pubs"
conn.open strConn
strSQL = "Select * From Employee ;"
rs=conn.Execute strSQL(注意次句)
形式4
set rs = Server.CreateObject("ADODB.RecordSet")
strConn ="driver={SQL Server};server=srv;"& _
"uid=sa;pwd=;database=pubs"
strSQL = "Select * From Employee ;"
rs.open strSQL,strConn,,,adCmdText
注:上面的例子中均假設(shè)SQL Server的name=srv,使用SQL Server Authentication采用了默認(rèn)的帳號(hào)sa,該帳號(hào)沒有設(shè)定密碼。
上面這幾種形式都能夠產(chǎn)生一個(gè)相同的RecordSet對(duì)象的實(shí)例rs,但方法各異,在后面的討論中我們將看到它們不同的優(yōu)越性.