1 Access的conn文件
最简单的写法(没有密码)
<%
db_path = "guestbook.mdb"
Set conn= Server.CreateObject("ADODB.Connection")
connstr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source="&Server.MapPath( db_path)
conn.Open connstr
%>
带有密码
<%
sqlUsername = "admin"
sqlPassword = "123456"
sqlDbPath = "data/db1.mdb"
ConStr = "Provider=Microsoft.Jet.OleDB.4.0; User ID = " & SqlUsername & "; Jet OleDB:Database Password = " & SqlPassword & "; Data Source = " & Server.MapPath(sqlDbPath)
On Error Resume Next
Set conn=server.createobject("adodb.connection")
conn.open ConStr
If Err Then
err.Clear
Set Conn = Nothing
Response.Write "数据库连接出错,请检查连接字串。"
Response.End
End If
Sub EndConnection
conn.close
Set conn=Nothing
End Sub
%>
2 sql的conn文件
<%
Response.Buffer = True
Response.ExpiresAbsolute = Now() - 1
Response.Expires = 0
Response.CacheControl = "no-cache"
'response.end
'end if
Session.Timeout=10
dim conn
dim connstr
on error resume next
'连接数据库
'---------------------------------------------------------------------------
connServerName = "(local)" '数据库服务器名或者IP,本机填“(local)”即可。
connDatabase = "yixunconnection" '数据库名称
connUserName = "sa" '数据库用户名
connPassword = "sa" '数据库密码
'---------------------------------------------------------------------------
Set conn = Server.CreateObject("ADODB.Connection")
connstr="Provider=SQLOLEDB;Data Source="&connServerName&";Initial Catalog="&connDatabase&";User ID="&connUserName&";Password="&connPassword&";"
if err.number<>0 then
err.clear
set conn=nothing
response.write "数据库连接出错!"
Response.End
else
conn.open connstr
if err then
err.clear
set conn=nothing
response.write "数据库连接出错!"
Response.End
end if
end if
sub endConnection()
conn.close
set conn=nothing
end sub
%>