写完系统了,放松一下,想想无聊,就将ASP中用到的数据库连接总结了一下,共享如下:
<%
dim conn '数据库连接变量
dim connstr '连接access数据库的字符串
Const isSqlDataBase = 0 '定义数据库类别,1为SQL数据库,0为Access数据库
If IsSqlDataBase = 1 Then
'-----------------------SQL数据库连接参数-------------------------------------
Dim SqlDatabaseName,SqlPassword,SqlUsername,SqlLocalName
SqlString = "GetDate()"
SqlDatabaseName = "datename" '数据库名
SqlPassword = "" '用户密码
SqlUsername = "sa" '用户名
SqlLocalName = "(local)" '连接名(本地用local,外地用IP)
ConnStr = "Provider = Sqloledb; User ID = " & SqlUsername & "; Password = " & SqlPassword & "; Initial Catalog = " & SqlDatabaseName & "; Data Source = " & SqlLocalName & ";"
Else
'-----------------------ACCESS数据库连接--------------------------------------
dim datepath '数据库路径
datepath = server.mappath("guestbook.mdb")
connstr = "provider=microsoft.jet.oledb.4.0;data source="&datepath
'connstr = "driver={microsoft access driver (*.mdb)};dbq="&datepath
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
%>
测试用数据库为guestbook.mdb
表为guestbook
各字段为
id 为自动生成的主键
name 文本 20
tel 数字
message 备注
time 日期/时间 默认值[=Now()]
<%
dim conn
set conn=server.createobject("adodb.connection")
conn.open "driver={microsoft access driver (*.mdb)};dbq="&server.mappath("test.mdb")
%>