不用DSN访问数据库(续)

王朝other·作者佚名  2006-12-16
窄屏简体版  字體: |||超大  

任何一个要访问数据库的ASP脚本都必须首先在服务器上打开数据库,

我们有两种方法:

.通过DSN建立连接

.不用DSN建立连接

一个DSN连接需要服务器的系统管理员在服务器上用控制面板中的ODBC

工具设置一个DSN,或者使用一个第三方的服务器组件,让你的ASP脚本

在需要时通过修改注册表建立DSN.我将提供一个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>

下面是典型的DRIVER参数值:

{Microsoft Access Driver (*.mdb)}

driver=SQL Server; server=127.0.0.1

&nbs

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
© 2005- 王朝網路 版權所有 導航