一個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>
下面是典型的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"
|