⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 0057.htm

📁 ASP教程宝典 书籍语言: 简体中文 书籍类型: 网络编程 授权方式: 免费软件 书籍大小: 500 KB
💻 HTM
字号:
<html>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<style>
<!--
body, table {font-size: 9pt; font-family: 宋体}
a {text-decoration:none}
a:hover {color: red;text-decoration:underline}
.1  {background-color: rgb(245,245,245)}
-->
</style>
<title>网络新时代,软件教程:操作系统 主页制作 服务器 设计软件 网络技术 编程语言 文字编辑</title>
</head>

<body>

<p align="center"><script src="../../1.js"></script> </p>

<p align="center"><big><strong>怎样用ASP访问SQL Server</strong></big></p>

<p align="right">---摘自《Microsoft》</p>

<pre>
The information in this article applies to:

Microsoft SQL Server versions 4.2x, 6.0, 6.5 
Microsoft Internet Information Server versions 3.0, 4.0 
Microsoft SQL Server 2000




SUMMARY
This article describes how to establish connections to SQL Server within an ASP script using ActiveX Data 
Objects (ADO), while taking advantage of the connection pooling feature of ODBC 3.0. 



MORE INFORMATION

Connection Pooling
Enable ODBC connection pooling. For general information on connection pooling and instructions on how to 
enable this feature, see the following article in the Microsoft Knowledge Base: 

Q164221 : How to Enable Connection Pooling in an ODBC Application 


ODBC DSN
Using the ODBC Administrator, create a System DSN on the computer where Microsoft Internet Information 
Server (IIS) is installed. Specify the connection attribute once and reuse it on every page. For example, 
in the Session_OnStart event within Global.asa, define the connection attribute as: 

   Session(&quot;ConnectionString&quot;) =
   &quot;dsn=SQLSysDSN;uid=sa;pwd=;DATABASE=pubs;APP=ASP Script&quot; 

Make sure all of the following conditions are true: 

The Trusted Connection box is not checked in the System DSN definition. 


The SQL Server security mode is not Windows NT Integrated. 


Within the connection attribute, the uid is not blank. 


Otherwise, a connection to SQL Server may fail with the following message: 

Microsoft OLE DB Provider for ODBC Drivers error '80004005'
[Microsoft][ODBC SQL Server Driver][SQL Server]Login failed- User: _
Reason: Not defined as a valid user of a trusted SQL Server connection. 


Global.asa
Using the Global.asa file is optional. In its place, entries usually made in this file can be put on the 
first page called by the application. Assuming the ASP scripts are located in a directory that is not 
defined as a virtual directory within the Internet Service Manager, but below another virtual directory, 
the Global.asa file containing Session variables and DSN definitions must be kept in the virtual 
directory. Otherwise, the following message will be returned: 

Microsoft OLE DB Provider for ODBC Drivers error '80004005'
[Microsoft][ODBC Driver Manager] Data source name not found and no
default driver specified 


Connections in the ASP Script
Take advantage of connection pooling by opening and closing the connection to the database on every active 
server page. To open the connection, type the following statements in the &lt;Body&gt; section of the page: 

   &lt;%
   Set OBJdbConnection = Server.CreateObject(&quot;ADODB.Connection&quot;)
   OBJdbConnection.Open Session(&quot;ConnectionString&quot;)
   %&gt; 

To close the connection, put the following immediately after the &lt;/Body&gt; tag: 

   &lt;%
   OBJdbConnection.Close
   Set OBJdbConnection = Nothing
   %&gt; 

You may encounter the following two error messages if the connection settings are not properly defined as 
outlined above: 

Microsoft OLE DB Provider for ODBC Drivers error '80004005'
[Microsoft][ODBC SQL Server Driver][DBNMPNTW]Connection broken. 
Microsoft OLE DB Provider for ODBC Drivers error '80004005'
[Microsoft][ODBC SQL Server Driver]Communication link failure 

Below is a sample application that consists of Global.asa and Authors.asp. This sample application will 
return four columns and all records in the pubs table called authors. 

Global.asa 


   &lt;SCRIPT LANGUAGE=VBScript RUNAT=Server&gt;
   Sub Session_OnStart
   Session(&quot;ConnectionString&quot;) =
   &quot;DSN=SQLSysDSN;UID=sa;PWD=;DATABASE=pubs;APP=ASP script&quot;
      Session(&quot;ConnectionTimeout&quot;) = 15
      Session(&quot;CommandTimeout&quot;) = 30
   End Sub

   Sub Session_OnEnd

   End Sub
   &lt;/SCRIPT&gt; 


Authors.asp 
   &lt;HTML&gt;
   &lt;HEAD&gt;
   &lt;TITLE&gt;All Authors&lt;/TITLE&gt;
   &lt;/HEAD&gt;
   &lt;BODY BGCOLOR=&quot;#FFFFFF&quot;&gt;

   &lt;% Set OBJdbConnection = Server.CreateObject(&quot;ADODB.Connection&quot;)
   OBJdbConnection.ConnectionTimeout = Session(&quot;ConnectionTimeout&quot;)
   OBJdbConnection.CommandTimeout = Session(&quot;CommandTimeout&quot;)
   OBJdbConnection.Open Session(&quot;ConnectionString&quot;)
   Set SQLStmt = Server.CreateObject(&quot;ADODB.Command&quot;)
   Set RS = Server.CreateObject (&quot;ADODB.Recordset&quot;)
   %&gt;

   &lt;p&gt;
   &lt;table border=&quot;0&quot; bordercolor=&quot;#000000&quot;&gt;
   &lt;%
   SQLStmt.CommandText = &quot;select * from authors&quot;
   SQLStmt.CommandType = 1
   Set SQLStmt.ActiveConnection = OBJdbConnection
   RS.Open SQLStmt

   Do While Not RS.EOF
   %&gt;
   &lt;TR&gt;
      &lt;TD Width = 150 ALIGN=LEFT&gt;
         &lt;FONT SIZE=+1&gt;
         &lt;%= RS(&quot;au_id&quot;) %&gt;
         &lt;/FONT&gt;&lt;/TD&gt;
      &lt;TD&gt;&lt;/TD&gt;
         &lt;TD Width = 150 ALIGN=LEFT&gt;
         &lt;FONT SIZE=+1&gt;
         &lt;%= RS(&quot;au_lname&quot;)  %&gt;
         &lt;/FONT&gt;&lt;/TD&gt;
      &lt;TD Width = 150 ALIGN=LEFT&gt;
         &lt;FONT SIZE=+1&gt;
         &lt;%= RS(&quot;au_fname&quot;)  %&gt;
         &lt;/FONT&gt;&lt;/TD&gt;
      &lt;TD Width = 150 ALIGN=LEFT&gt;
         &lt;FONT SIZE=+1&gt;
         &lt;%= RS(&quot;phone&quot;)  %&gt;
         &lt;/FONT&gt;&lt;/TD&gt;
   &lt;/TR&gt;
   &lt;%
   RS.MoveNext
   Loop
   %&gt;
   &lt;/table&gt;
   &lt;hr&gt;
   &lt;p&gt;
   &lt;/BODY&gt;
   &lt;% OBJdbConnection.Close
   Set OBJdbConnection = Nothing
   %&gt;
   &lt;/HTML&gt; 

For more information on Active Server Pages, refer to the Roadmap provided by the ASP setup program on the 
IIS server. 

Additional query words: browser explorer web sqlfaqtop 

</pre>

<p align="center"><script src="../../2.js"></script> </p>
</body>
</html>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -