动网论坛,站长建站首选,国内使用量最多的论坛软件 动网论坛官方技术讨论区 站长工具 申请属于您自己的免费论坛
首页 | 新闻资讯 | 网站运营 | 网络编程 | 数据库 | 服务器 | 网页设计 | 图像媒体 | 网络应用 | 搜索优化 | 资源下载 | 动网主机 | DVBOX
    本站内  互联网 ASP论坛  ASP.Net论坛  PHP论坛
   Asp → 阅读文章

 另类: asp不用DSN访问数据库

作者来源: 
阅读 数 495 人次 , 2006-4-18 5:01:00 

  一个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"
  
 本文Tags数据库  
 收藏本文  打印本文  论坛讨论  关闭窗口
· 上一篇:应用总结:ASP编程心得回放
· 下一篇:精心收集的近百个ASP编程网址
· 数字字串(钱)转换为中文(大写)
· 在linux上使用ASP
· 发送信息页面send.asp--基于web的QQ程序3
· ASP进阶之文章在线管理更新(十)
· asp实现数据记录的备份及恢复-----抛砖引玉


关于本站 | 联系我们 | 业务合作 | 客户案例 | 诚聘英才 | 广告合作 | 收藏本站
海口动网先锋网络科技有限公司版权所有
Copyright © 2000 - 2006 Cndw.Com
中华人民共和国电信与信息服务业务经营许可证编号 琼 ICP 020077