ASP連接數(shù)據(jù)庫的全能代碼
發(fā)表時(shí)間:2024-02-02 來源:明輝站整理相關(guān)軟件相關(guān)文章人氣:
[摘要]'解決了ACCESS數(shù)據(jù)庫路徑的問題!'采用DBType=0或DBType=1來區(qū)分AC庫還是MSSQL庫'具體采用AC庫時(shí)應(yīng)注意的事項(xiàng),請看程序說明-----------------------------------------------Dim DBType,Conn...
'解決了ACCESS數(shù)據(jù)庫路徑的問題!
'采用DBType=0或DBType=1來區(qū)分AC庫還是MSSQL庫
'具體采用AC庫時(shí)應(yīng)注意的事項(xiàng),請看程序說明
-----------------------------------------------
Dim DBType,Conn,StrConn
DBType=0 '0為Access數(shù)據(jù)庫,1為MSSQL數(shù)據(jù)庫
If(DBType=0) Then
'****************ACCESS數(shù)據(jù)庫******************
Dim DbFolderName,DbFolder_Path,SiteFolder
DbFolderName="ArticleData" '數(shù)據(jù)庫所在文件夾名稱
DbFolder_Path = Server.MapPath(DbFolderName) '數(shù)據(jù)庫所在路徑
SiteFolder="Article" '系統(tǒng)所在根目錄名稱
If Session("RootDir") = "" Then
Session("RootDir") = Mid(DbFolder_Path, 1, InStr(1,DbFolder_Path,SiteFolder,1) -1) & SiteFolder
End if
Set Conn = Server.CreateObject("Adodb.Connection")
StrConn = "Driver={Microsoft Access Driver (*.mdb)};DBQ=" & Session("RootDir") & "\"& DbFolderName & "\Data.mdb" '連接到數(shù)據(jù)庫
Conn.Open StrConn
'**************************************************
ElseIf(DBType=1) Then
'*****************MSSQL SERVER數(shù)據(jù)庫******
Dim DBUserID,DBPassWord,DBName,DBIP
'修改以下信息以適合你的網(wǎng)站
DBUserID="sa" '數(shù)據(jù)庫登陸名
DBPassWord="" '數(shù)據(jù)庫密碼
DBName="dbname" '數(shù)據(jù)庫名稱
DBIP="local" '數(shù)據(jù)庫所在地址,如果是本地?cái)?shù)據(jù)庫則為:(local)
Set Conn=Server.CreateObject("Adodb.Connection")
StrConn = "PROVIDER=SQLOLEDB.1;Data Source="&DBIP&";Initial Catalog="&DBName&";Persist Security Info=True;User ID="&DBUserID&";Password="&DBPassWord&";Connect Timeout=30"
Conn.Open StrConn
'******************************************************
Else
'***********************數(shù)據(jù)庫設(shè)置錯(cuò)誤*****************
Response.Write"數(shù)據(jù)庫設(shè)置錯(cuò)誤,請聯(lián)系管理員!"
Response.End
End If
'Response.Write StrConn