扫一扫
分享文章到微信
扫一扫
关注官方公众号
至顶头条
作者:csbq 来源:论坛总结 2007年9月25日
关键字:
一个DSN连接通常需要的参数有:DSN名,用户名,口令,例如我们用用户名"student",口令"magic",通过DSN"student"建立连接:
1. set conntemp=server.createobject("adodb.connection")
2. conntemp.open "DSN=Student; uid=student; pwd=magic"
3. set rstemp=conntemp.execute("select * from authors")
如果我们没有DSN,该怎么做呢?
但是我们知道文件名(比如,Access,Paradox,FoxPro的数据库)或者数据源名(例如,SQLserver的数据库).这里有一个方法,我们不要DSN就可以访问数据库.注意,你必须知道实际的文件路径!比如: "C:\thatserver\account17\nwind.mdb".
幸好,方法 server.mappath 可以返回服务器上的地址.
1. set conntemp=server.createobject("adodb.connection")
2. cnpath="DBQ=" & server.mappath("yourtable.mdb")
3. conntemp.Open "DRIVER={Microsoft Access Driver (*.mdb)}; " & cnpath
4. set rstemp=conntemp.execute("select * from authors")
<HTML><HEAD>
<TITLE>nwind.asp</TITLE>
<body bgcolor="#FFFFFF"></HEAD>
<%
set conntemp=server.createobject("adodb.connection")
'' 不用DSN建立连接
DSNtemp="DRIVER={Microsoft Access Driver (*.mdb)}; "
DSNtemp=dsntemp & "DBQ=" & server.mappath("nwind.mdb")
conntemp.Open DSNtemp
'' 不用DSN建立连接
set rstemp=conntemp.execute("select * from customers where country=''germany''")
howmanyfields=rstemp.fields.count -1
%>
<table border=1>
<tr>
<% ''Put Headings On The Table of Field Names
for i=0 to howmanyfields %>
<td><b><%=rstemp(i).name %></B></TD>
<% next %>
</tr>
<% '' Now lets grab all the records
do while not rstemp.eof %>
<tr>
<% for i = 0 to howmanyfields%>
<td valign=top><%=rstemp(i)%></td>
<% next %>
</tr>
<% rstemp.movenext
loop
rstemp.close
set rstemp=nothing
conntemp.close
set conntemp=nothing %>
</table>
</BODY>
</HTML>
下面是典型的DRIVER参数值:
{Microsoft Access Driver (*.mdb)}
driver=SQL Server; server=127.0.0.1
^ SQLServer的IP地址
不通过数据源访问SQL和ACCESS
Using SQL Server 6.5:
set Conn = Server.CreateObject("ADODB.Connection")
Conn.Open "driver=SQL Server; server=server_name; uid=your_UID; pwd=your_PW; database=your_database;"
Using Access:
set Conn = Server.CreateObject("ADODB.Connection")
Conn.Open "DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=c:\www\db\guestbook.mdb"
如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。
现场直击|2021世界人工智能大会
直击5G创新地带,就在2021MWC上海
5G已至 转型当时——服务提供商如何把握转型的绝佳时机
寻找自己的Flag
华为开发者大会2020(Cloud)- 科技行者