Option Explicit
Dim rp As Response
Dim rq As Request
Dim ap As Application
Dim sr As Server
Dim sn As Session
Dim Conn As ADODB.Connection ''創(chuàng)建conn全局變量
Dim RS As ADODB.Recordset '' 創(chuàng)建全避變量rs
Public Sub OnStartPage(MyScriptingContext As ScriptingContext)
Set rp = MyScriptingContext.Response
Set rq = MyScriptingContext.Request
Set sr = MyScriptingContext.Server
Set ap = MyScriptingContext.Application
Set sn = MyScriptingContext.Session
End Sub
Public Sub OnEndPage()
Set rp = Nothing
Set rq = Nothing
Set sr = Nothing
Set ap = Nothing
Set sn = Nothing
End Sub
Public Function openConn()
Dim str As String
Set Conn = sr.CreateObject("ADODB.Connection")
str = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & sr.MapPath("/mssdatabase/dbtest.mdb") &";data password=123456789"
Conn.Open str
If Err.Number <> 0 Then ''就是在社里返回喔
openConn = False
Else
openConn = True
End If
End Function
Public Function execute(sql) As ADODB.Recordset ''創(chuàng)建一個(gè)execute的方法,返回為recordset對像
Set RS = New ADODB.Recordset
RS.CursorLocation = adUseClient
Call openConn ''這句得要加,好像在外部的asp中不能觸發(fā)全局conn變量,不知為此??
RS.Open sql, Conn, 3, 3
Set execute = RS
End Function
編譯該工程時(shí),切記引用(工程>引用)"Microsoft active server pages object library"和"microsoft activexX data object 2.6 library"兩個(gè)庫類(感覺像c#的using差不多一樣),然后編譯成dll(文件>生成myconn.dll),一般vb編譯成dll都會在系統(tǒng)中自動(dòng)注冊,如果是ntsf分區(qū),給該dll加個(gè)everyone 可讀,執(zhí)行權(quán)限,然后到asp里調(diào)用,實(shí)現(xiàn)代碼如下:
<%
dim conn
dim dbLink
dbLink="2"
if dbLink="1" then ''如果代碼在別的機(jī)器上運(yùn)行就使用dll組件鏈接數(shù)據(jù)庫,否則就使用本地鏈接
Set conn=Server.CreateObject("myconn.dbconn")
conn.openConn
else
set conn=Server.CreateObject("ADODB.CONNECTION")
DBPath = Server.MapPath("/mssdatabase/dbtest.mdb")
conn.open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source="& DBPath
end if
%>
由于兩種鏈接方式都只是使用了一個(gè)conn對像,所以不用對其他代碼進(jìn)行大修改,還是比較方便,目前dll中的execute方法對select操作已經(jīng)測試通過,還有insert、update、delete等方法有待完善。
附件為myconn工程文件全部代碼
聯(lián)系客服