当前位置: 首页 > 图文教程 > 网络编程 > ASP > asp不用DSN访问数据库

ASP
提高SQL的执行效率的ASP的五种做法
asp的offset的一个go to page
asp中command的在单条记录时,有些字段显示为空的问题
asp,php一句话木马整理方便查找木马
asp 合并记录集并删除的sql语句
asp下循环一行多少个
asp循环行数输出函数
asp access重新开始编号
ASP生成数字相加求和的BMP图片验证码
静态页面利用JS读取cookies记住用户信息
比较详细的Asp伪静态化方法及Asp静态化探讨
asp Response.flush 实时显示进度
Asp高级故障解决以及相关代码
asp智能脏话过滤系统v1.0
ASP文件中的安全问题
Access数据库中“所有记录中均未找到搜索关键字”的解决方法
asp两组字符串数据比较合并相同数据
asp MYSQL出现问号乱码的解决方法
asp文章中随机插入网站版权文字的实现代码
ASP利用XMLHTTP实现表单提交以及cookies的发送的代码

ASP 中的 asp不用DSN访问数据库


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-11-03   浏览: 94 ::
收藏到网摘: n/a

一个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"