ASP串連Access資料庫的幾種方法
來源:互聯網
上載者:User
1. 相對與比較老的環境,建議使用第二個
set dbconnection=Server.CREATEOBJECT("ADODB.CONNECTION")
DBPath = Server.MapPath("customer.mdb")
dbconnection.Open "driver={Microsoft Access Driver (*.mdb)};dbq=" & DBPath
SQL="select * from auth where id='" & user_id &"'"
SET uplist=dbconnection.EXECUTE(SQL)
2.win2003以上的機器,建議用此方法,效率更高
set dbconnection=Server.CreateObject("ADODB.Connection")
DBPath = Server.MapPath("customer.mdb")
dbconnection.Open "provider=microsoft.jet.oledb.4.0;data source="&dbpath
SQL="select * from auth where id='" & user_id &"'"
SET uplist=dbconnection.EXECUTE(SQL)
3.
DBPath = Server.MapPath("customer.mdb")
set session("rs")=Server.CreateObject("ADODB.Recordset")
' rs=Server.CreateObject("ADODB.Recordset")
connstr="provider=microsoft.jet.oledb.4.0;data source="&dbpath
SQL="select * from auth where id='" & user_id &"'"
session("rs").Open sql,connstr,1,3
4.
建odbc源xxx
set conn=server.createobject("Adodb.connection")
conn.open "DSN=xxx;UID=;PWD=;Database=customer
5、附上一個經常用的sqlserver與access通用的串連資料庫代碼
複製代碼 代碼如下:<%
Const DataBaseType=1
If DataBaseType=0 then
DBPath="/jb51/news.asp"
SqlNowString = "Now()"
ystr=true
nstr=false
suiji="rnd(id)"
Else
'如果是SQL資料庫,請認真修改好以下資料庫選項
DataServer = "wwwjb51net" '資料庫伺服器IP
DataUser = "jb51net" '訪問資料庫使用者名稱
DataBaseName = "jb51net" '資料庫名稱
DataBasePsw = "密碼" '訪問資料庫密碼
SqlNowString = "getdate()"
ystr=1
nstr=0
suiji="newid()"
End if
On Error Resume Next
If DataBaseType = 1 Then
ConnStr="driver={SQL Server};server="&dataserver&";UID="&datauser&";PWD="&databasepsw&";Database="&databasename
Else
ConnStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Server.MapPath(DBPath)
End If
Set conn = Server.CreateObject("ADODB.Connection")
conn.open ConnStr
If Err Then Err.Clear:Set conn = Nothing:Response.Write "資料庫連接出錯,請檢查Conn.asp檔案中的資料庫參數設定。":Response.End
%>