下面提供一款最全面的asp資料庫連接源碼哦,可以根據不同類型判斷是串連access資料庫還是mssql資料庫哦。
<%@LANGUAGE="VBSCRIPT" CODEPAGE="936"%>
<%
dim DataLinkType,conn,connstr,MyDbPath
MyDbPath="data/Database.mdb" '資料庫檔案位置
DataLinkType = 0 '設定資料庫連接方式,1為SQL資料庫,0為Access資料庫
If dataLinkType=0 Then
'**************************************************
'使用Access資料庫
'**************************************************
ConnStr = "Provider = Microsoft.Jet.OLEDB.4.0;Data Source = " & Server.MapPath(MyDbPath)
Else
'**************************************************
'使用SQL資料庫
'**************************************************
'ConnStr = "Driver={SQL Server}; server=(local);uid=sa;pwd=windows;database=data1;"
ConnStr = "Provider=SQLNCLI; server=.SQLEXPRESS;uid=sa;pwd=windows;database=data1;"
End If
On Error Resume Next
Set conn = Server.CreateObject("ADODB.Connection")
conn.open ConnStr
If Err Then
Err.Clear
Set Conn = Nothing
Response.Write "資料庫連接出錯,請檢查串連字串。"'注釋,需要把這幾個字翻譯成英文。
Response.End
End If
'**************************************************
'關閉資料庫
'**************************************************
sub CloseConn()
conn.close
set conn=nothing
end sub
%>