ASP连接Access数据库的几种方法

所属分类: 网络编程 / ASP编程 阅读数: 933
收藏 0 赞 0 分享

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 
%> 

完整测试代码

Microsoft Access Driver

<%
db="1.mdb"
dim conn,db 
dim connstr 
on error resume next 
connstr="DBQ="+server.mappath(""&db&"")+";DefaultDir=;DRIVER={Microsoft Access Driver (*.mdb)};" 
set conn=server.createobject("ADODB.CONNECTION") 
if err then 
err.clear 
else 
conn.open connstr 
end if 

t=timer()
for i=0 to 1000
	Set Res=Conn.Execute("select (Select top 1 name from [aa] where id=1) as oldname,(Select top 1 name from [aa] where id=2) as newname from [aa]")
	aa=Res("oldname")
	bb=Res("newname")
	Res.close
	set Res=nothing
next
response.write "用时:"&(timer()-t)
%> 

microsoft.jet.oledb.4.0

<%
db="1.mdb"
dim conn,db 
dim connstr 
on error resume next 
connstr="provider=microsoft.jet.oledb.4.0;data source="+server.mappath(""&db&"")
set conn=server.createobject("ADODB.CONNECTION") 
if err then 
err.clear 
else 
conn.open connstr 
end if 

t=timer()
for i=0 to 1000
	Set Res=Conn.Execute("select (Select top 1 name from [aa] where id=1) as oldname,(Select top 1 name from [aa] where id=2) as newname from [aa]")
	aa=Res("oldname")
	bb=Res("newname")
	Res.close
	set Res=nothing
next
response.write "用时:"&(timer()-t)
%> 
更多精彩内容其他人还在看

asp中去除内容HTML标签的三个function函数

asp中去除内容HTML标签的三个function函数
收藏 0 赞 0 分享

chr(9)、chr(10)、chr(13)、chr(32)、chr(34)讲解

chr(9)、chr(10)、chr(13)、chr(32)、chr(34)讲解
收藏 0 赞 0 分享

方便的大家admin及admin888 经过 md5加密后16位和32位代码

方便的大家admin及admin888 经过 md5加密后16位和32位代码
收藏 0 赞 0 分享

可用的ASP无重复数字随机函数, 数组实现, 并应用于随机显示记录集

可用的ASP无重复数字随机函数, 数组实现, 并应用于随机显示记录集
收藏 0 赞 0 分享

asp动态级联菜单代码

asp动态级联菜单代码
收藏 0 赞 0 分享

ASP中经常使用的SQL语句与教程说明

ASP中经常使用的SQL语句与教程说明
收藏 0 赞 0 分享

实例分析之用ASP编程实现网络内容快速查找的代码

实例分析之用ASP编程实现网络内容快速查找的代码
收藏 0 赞 0 分享

服务端 VBScript 与 JScript 几个相同特性的写法与示例

服务端 VBScript 与 JScript 几个相同特性的写法与示例
收藏 0 赞 0 分享

ASP 环境下 VBS 事件应用 示例代码

ASP 环境下 VBS 事件应用 示例代码
收藏 0 赞 0 分享

asp 之上传漏洞终结篇

asp 之上传漏洞终结篇
收藏 0 赞 0 分享
查看更多