將連接數(shù)據(jù)庫(kù)的代碼隱藏在DLL中
發(fā)表時(shí)間:2024-06-06 來(lái)源:明輝站整理相關(guān)軟件相關(guān)文章人氣:
[摘要]其實(shí),大家在書(shū)寫(xiě)asp程序時(shí),最擔(dān)心的可能就是數(shù)據(jù)庫(kù)的連接信息,會(huì)被一些人看到,進(jìn)行一些破壞。下面我們用vb6.0來(lái)做一個(gè)動(dòng)態(tài)鏈接庫(kù)(.dll)來(lái)隱藏?cái)?shù)據(jù)庫(kù)的連接信息。 啟動(dòng)vb6.0,新建-->Active dll工程。單擊"工程"-->引用,選擇"...
其實(shí),大家在書(shū)寫(xiě)asp程序時(shí),最擔(dān)心的可能就是數(shù)據(jù)庫(kù)的連接信息,會(huì)被一些人看到,進(jìn)行一些破壞。下面我們用vb6.0來(lái)做一個(gè)動(dòng)態(tài)鏈接庫(kù)(.dll)來(lái)隱藏?cái)?shù)據(jù)庫(kù)的連接信息。
啟動(dòng)vb6.0,新建-->Active dll工程。單擊"工程"-->引用,選擇"microsoft active server pages object library"
和"microsoft activeX data objects 2.1 library"兩項(xiàng)。將類模塊的名稱改為dcss.將工程的名稱改為yygwy.保存工程文件yygwy.vbp和類文件dcss.cls。
在dcss.cls中寫(xiě)入:
Private myscriptingcontext As ScriptingContext
Private myapplication As Application
Private myrequest As Request
Private myresponse As Response
Private myserver As Server
Private mysession As Session
Public Sub onstartpage(passedscriptingcontext As ScriptingContext)
Set myscriptingcontext = passedscriptingcontext
Set myapplication = myscriptingcontext.Application
Set myrequest = myscriptingcontext.Request
Set myresponse = myscriptingcontext.Response
Set myserver = myscriptingcontext.Server
Set mysession = myscriptingcontext.Session
End Sub
Public Sub onendpage()
Set myscriptingcontext = Nothing
Set myapplication = Nothing
Set myrequest = Nothing
Set myresponse = Nothing
Set myserver = Nothing
Set mysession = Nothing
End Sub
'以上語(yǔ)句是必須的。
'定義兩個(gè)公有函數(shù)
Public Function rsresult(strsql As String) As Recordset
Dim mycnn As Connection
Dim myset As Recordset
Dim strconnstring As String
'strconnstring = "provider=sqloledb.1;
password=;" & "user id=sa;" & "initial catalog=vlog;" & "data source=hpe60;
connect timeout=15"
strconnstring = "driver={sql server};server=yang;uid=sa;pwd=;
database=dcss"
'mycnn.ConnectionString = strconnstring
mycnn.Open strconnstring
myset.ActiveConnection = mycnn
myset.Open strsql, mycnn, 3, adCmdText
Set rsresult = myset
End Function
Public Function datasource() As Variant
datasource = "driver={sql server};server=yang;uid=sa;pwd=; database=dcss"
End Function
編譯生成dcss.dll文件。注冊(cè)regsvr32 路徑\dcss.dll。
用visual interdev打開(kāi)global.asa文件.當(dāng)然了,你也可以在其它文件中使用。
set dcss=server.CreateObject("yygwy.dcss")
oconn=dcss.datasource()
application("strconn")=oconn
在其它的頁(yè)面中如下調(diào)用即可:
set objConn = Server.CreateObject("ADODB.Connection")
objConn.Open application("strconn")