一个dsn连接需要服务器的系统管理员在服务器上用控制面板中的odbc工具设置一个dsn,或者使用一个第三方的服务器组件,让你的asp脚本在需要时通过修改注册表建立dsn.
一个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>
Java Asp PHP .Net XML C/C++ CGI VB Jsp J2ee J2se J2me EJB Servlet Tomcat Resin Struts Weblogic Eclipse ANT GUI JMS Web servise IDEA Webphere Hibernate Spring Jboss Applet Swing Socket Javamail Perl Ajax P2P 安全 模式 框架 测试 开源 游戏
Windows XP Windows 2000 Windows 2003 Windows Me Windows 9.x Linux UNIX 注册表 操作系统 服务器 应用服务器