概要
      本文介绍一种当使用 Microsoft VBScript 和 Microsoft JScript 编程时,在 Active Server Page (ASP) 中创建 ActiveX 数据对象 (ADO) 非连接记录集的方法。本文假定读者熟悉 ADO 和 ASP。 
更多信息
ASP 的原则之一是尽快释放 ADO 对象,从而释放这些对象所使用的系统资源。ADO 非连接记录集这一功能允许记录集在没有活动连接时也能存在;这可以节省数据库服务器资源并提高伸缩性。ADO 非连接记录集要求使用客户端游标,这可以通过将Connection 对象的 CursorLocation 属性设置为adUseClient 来实现。下面的示例代码使用 Microsoft OLEDB Provider for SQL Server (SQLOLEDB) 连接到随 SQL Server 安装一起提供的 Northwind 示例数据库。要运行这些示例,需修改连接字符串和 SELECT 语句以适应您的环境。另外,还要为 ADO 常量的 Include 文件设置正确的路径。 
VBScript 中的非连接记录集

  1. <%@Language="VBScript"%>  
  2. <!-- Include file for VBScript ADO Constants -->  
  3. <!--#include File="adovbs.inc"-->  
  4. <%  
  5.     ' Connection string.  
  6.     strCon = "Provider=sqloledb;Data Source=myServer;Initial Catalog=Northwind;User Id=myUser;Password=myPassword" 
  7.  
  8.     ' Create the required ADO objects.  
  9.     Set conn = Server.CreateObject("ADODB.Connection")  
  10.     Set rs = Server.CreateObject("ADODB.recordset")  
  11.  
  12.     ' Open the connection.  
  13.     conn.Open strCon  
  14.  
  15.     ' Retrieve some records.  
  16.     strSQL = "Select * from Shippers" 
  17.     rs.CursorLocation = adUseClient  
  18.     rs.Open strSQL, conn, adOpenStatic, adLockOptimistic  
  19.  
  20.     ' Disconnect the recordset.  
  21.     Set rs.ActiveConnection = Nothing 
  22.  
  23.     ' Release the connection.  
  24.     conn.Close  
  25.  
  26.     ' Check the status of the connection.  
  27.     Response.Write("<BR> Connection.State = " & conn.State)  
  28.  
  29.     Set conn = Nothing 
  30.  
  31.     ' Use the diconnected recordset here.  
  32.  
  33.     ' Release the recordset.  
  34.     rs.Close  
  35.     Set rs = Nothing 
  36. %>  

                
注意,记录集是通过将ActiveConnection 属性设置为 Nothing 断开连接的。 
JScript 中的非连接记录集

  1. <%@Language="JScript"%>  
  2. <!-- Include file for JScript ADO Constants -->  
  3. <!--#include File="adojavas.inc"-->  
  4. <%  
  5.     // Connection string.  
  6.     var strCon = "Provider=sqloledb;Data Source=myServer;Initial Catalog=Northwind;User Id=myUser;Password=myPassword";  
  7.  
  8.     // Create the required ADO objects.  
  9.     conn = Server.CreateObject("ADODB.Connection");  
  10.     rs = Server.CreateObject("ADODB.recordset");  
  11.  
  12.     // Open the connection.  
  13.     conn.Open(strCon);  
  14.  
  15.     // Retrieve some records.  
  16.     var strSQL = "Select * from Shippers";  
  17.     rs.CursorLocation = adUseClient;  
  18.     rs.Open(strSQL, conn, adOpenStatic, adLockOptimistic);  
  19.  
  20.     // Disconnect the recordset.  
  21.     DisconnectRecordset(rs);  
  22.  
  23.     // Release the connection.  
  24.     conn.Close();  
  25.  
  26.     // Check the status of the connection.  
  27.     Response.Write("<BR> Connection.State = " + conn.State);  
  28.  
  29.     conn = null;  
  30.  
  31.     // Use the diconnected recordset here.  
  32.  
  33.     // Release the recordset.  
  34.     rs.Close();  
  35.     rs = null;  
  36. %>  
  1. <SCRIPT LANGUAGE="VBScript" RUNAT="SERVER">  
  2. Sub DisconnectRecordset(rs)  
  3.     Set rs.ActiveConnection = Nothing 
  4. End Sub 
  5. </SCRIPT>  

                
备注:在前面的代码中,不能将以下代码行     

  1. DisconnectRecordset(rs);  

               
替换为以下某个代码行来创建断开连接的记录集:    

  1. rs.ActiveConnection = null; 

                
- 或 - 

  1. delete(rs.ActiveConnection);  

                
有另外一种方法可以创建非连接记录集。JScript 中没有与 VBScript 中的Nothing 关键字(用来释放 ActiveX 对象)类似的关键字。要实现这一点,可以使用与下面的 Web 站点中提供的方法类似的方法: 
http://www.netspace.net.au/~torrboy/code/jargutil
在本例中,示例代码可能类似于下面这样:     

  1. var oUtil = Server.CreateObject("Torrboy.JArgUtility");  
  2.     rs.ActiveConnection = oUtil.Nothing;  

                
参考
有关其他信息,请单击下面的文章编号,查看 Microsoft 知识库文章: 
184397 HOWTO:Create ADO Disconnected Recordsets in VBA/C++/Java(HOWTO:在 VBA/C++/Java 中创建 ADO 断开连接的记录集) 
190717 INFO:Disconnected Recordsets with ADO or RDS(INFO:ADO 或 RDS 的断开连接的记录集) 
252482 BUG:ADO Disconnected Recordset That Uses Parameterized Query Is Not Disconnected by SQL Server(BUG:使用参数化查询的 ADO 断开连接的记录集没有被 SQL Server 断开连接)

http://support.microsoft.com/kb/289531/en-us

此文章由 flyinweb 于 2010-11-24 17:31:06 编辑

本日志由 flyinweb 于 2009-06-19 12:49:10 发表,目前已经被浏览 4299 次,评论 0 次;

作者添加了以下标签: ADO 记录集Recordset

引用通告:http://www.517sou.net/Article/36/Trackback.ashx

评论订阅:http://www.517sou.net/Article/36/Feeds.ashx

评论列表

    暂时没有评论
(必填)
(必填,不会被公开)