Warning: Undefined variable $self_hreflang in /www/wwwroot/www.che0.com/wp-content/themes/huaxia/functions.php on line 2130

Warning: Undefined variable $self_hreflang in /www/wwwroot/www.che0.com/wp-content/themes/huaxia/functions.php on line 2130

Warning: Undefined variable $self_hreflang in /www/wwwroot/www.che0.com/wp-content/themes/huaxia/functions.php on line 2154
Content

ASP连接Access数据库的几种方法(asp连接数据库的步骤)硬核推荐

文章摘要

这篇文章介绍了几种在不同环境中使用ADOBE database connectivity组件连接到Microsoft Access数据库的方法。以下是主要内容的总结: 1. **在相对老的环境中建议使用第二个方法**: 使用`Set DBConnection=Server.CREATEOBJECT("ADODB.CONNECTION")`和`DBPath=Server.MapPath("customer.mdb")`,通过执行SQL语句来获取数据。 2. **在Windows 2003以上的机器上建议使用第二个方法**: 使用`Set DBConnection=Server.CreateObject("ADODB.Connection")`,并提供数据源路径,同样通过`Execute`方法获取数据。 3. **在使用ADOBE Recordset时建议使用第三个方法**: 创建记录集对象,设置连接字符串,并使用`Open`方法连接到数据库。 4. **创建ODBC源时建议使用第四个方法**: 使用`Set conn=server.createobject("Adodb.connection")`打开DSN连接字符串,连接到指定数据库。 5. **提供一个通用的SQL Server与Access连接代码**: 根据用户需求,可以根据SQL Server或其他数据库类型调整配置参数,实现通用连接。 文章还测试了前两种方法的性能,结果显示第二个方法在不同环境中表现更好。文章总结了各种方法的适用场景,并附上了完整的测试代码供参考。更多相关资源和文章请访问脚本之家。



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连接Access数据库的几种方法的文章就介绍到这了,更多相关ASP连接Access数据库内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:用ASP实现在线压缩与解压缩功能代码用ASP.Net实现文件的在线压缩和解压缩ASP 连接Access数据库的登陆系统asp连接access、sql数据库代码及数据库操作代码access数据库的一些少用操作,ASP,创建数据库文件,创建表,创建字段,ADOX深入浅析ASP在线压缩access数据库的方法