資料|資料庫|資料庫連接 各種資料庫連接代碼
今天獃著沒事幹,把各種資料庫的串連總結了一下,現拿出來與大家分享:
MS Access資料庫連接
用DSN串連並且沒有使用者名稱和密碼:
<%
set conn = Server.CreateObject("ADODB.Connection")
conn.open "YourDSNName"
%>
用DSN串連並且有使用者名稱和密碼:
<%
set conn = Server.CreateObject("ADODB.Connection")
conn.open "YourDSNName","username","password"
%>
用實際的資料庫絕對路徑串連:
<%
Set conn = Server.CreateObject("ADODB.Connection")
Strconn="DRIVER={Microsoft Access Driver (*.mdb)}; "
Strconn=Strconn & "DBQ=e:\yanhang\database.mdb"
conn.Open Strconn
%>
用實際的資料庫相對路徑串連:
<%
Set conn = Server.CreateObject("ADODB.Connection")
Strconn="DRIVER={Microsoft Access Driver (*.mdb)}; "
Strconn=Strconn & "DBQ=" & Server.MapPath("/database/yanhang.mdb")
conn.Open Strconn
%>
MS SQL Server資料庫連接
用DSN串連:
<%
set conn = Server.CreateObject("ADODB.Connection")
conn.open "DSN=MyDSN;UID=user;PWD=password;DATABASE=databasename"
%>
不用DSN串連:
<%
Set conn = Server.CreateObject("ADODB.Connection")
DSNtemp="DRIVER={SQL Server};SERVER=ServerName;UID=USER;PWD=password;DATABASE=databasename"
conn.open DSNtemp
%>
FoxPro資料庫連接
<%
Set Conn = Server.CreateObject("ADODB.connection")
ConnStr= "Driver=Microsoft Visual Foxpro Driver; UID=userID;SourceType=DBC;SourceDB=C:\yanhang\database.dbc"
Conn.Open ConnStr
%>
Oracle資料庫連接:
<%
set conn=server.createobject("adodb.connection")
conn.cursorlocation=adUseClient
DSNTemp="Provider=MSDAORA.1;Password=xxxxx;User ID=yanhang;Data Source=xxx.world"
conn.open DSNtemp
%>