How To Create ADO Disconnected Recordsets in ASP Using VBScript and JScript

Article translations Article translations
Article ID: 289531 - View products that this article applies to.
This article was previously published under Q289531
Expand all | Collapse all

Summary

This article shows one technique to create ActiveX Data Objects (ADO) disconnected recordsets in Active Server Pages (ASP) when programming with Microsoft VBScript and Microsoft JScript. This article assumes that the reader is familiar with ADO and ASP.

More information

One of the ASP guidelines is to release ADO objects as soon as possible in order to release the system resources used by these objects. The disconnected recordset feature of ADO allows recordsets to exist without having an active connection; this can save database server resources and improve scalability. ADO disconnected recordsets require client-side cursors to be used, which can be done by setting the CursorLocation property of the Connection object to adUseClient.

The following sample code uses the Microsoft OLEDB Provider for SQL Server (SQLOLEDB) to connect to the sample Northwind database that comes with the SQL Server installation. To run the samples, modify the connection string and SELECT statement to suit your environment. Also, set the correct path for the ADO constants Include files.

Disconnected Recordsets in VBScript

<%@Language="VBScript"%>
<!-- Include file for VBScript ADO Constants -->
<!--#include File="adovbs.inc"-->
<%
	' Connection string.
	strCon = "Provider=sqloledb;Data Source=myServer;Initial Catalog=Northwind;User Id=myUser;Password=myPassword"
		   
	' Create the required ADO objects.
	Set conn = Server.CreateObject("ADODB.Connection")
	Set rs = Server.CreateObject("ADODB.recordset")
		   
	' Open the connection.
	conn.Open strCon
	
	' Retrieve some records.
	strSQL = "Select * from Shippers"
	rs.CursorLocation = adUseClient
	rs.Open strSQL, conn, adOpenStatic, adLockOptimistic

	' Disconnect the recordset.
	Set rs.ActiveConnection = Nothing
		
	' Release the connection.
	conn.Close

	' Check the status of the connection.
	Response.Write("<BR> Connection.State = " & conn.State)

	Set conn = Nothing
	
	' Use the diconnected recordset here.

	' Release the recordset.
	rs.Close
	Set rs = Nothing
%>
				
Note that the recordset is disconnected by setting the ActiveConnection property to Nothing.

Disconnected Recordsets in JScript

<%@Language="JScript"%>
<!-- Include file for JScript ADO Constants -->
<!--#include File="adojavas.inc"-->
<%
	// Connection string.
	var strCon = "Provider=sqloledb;Data Source=myServer;Initial Catalog=Northwind;User Id=myUser;Password=myPassword";
		   
	// Create the required ADO objects.
	conn = Server.CreateObject("ADODB.Connection");
	rs = Server.CreateObject("ADODB.recordset");
		   
	// Open the connection.
	conn.Open(strCon);
	
	// Retrieve some records.
	var strSQL = "Select * from Shippers";
	rs.CursorLocation = adUseClient;
	rs.Open(strSQL, conn, adOpenStatic, adLockOptimistic);

	// Disconnect the recordset.
	DisconnectRecordset(rs);
	
	// Release the connection.
	conn.Close();

	// Check the status of the connection.
	Response.Write("<BR> Connection.State = " + conn.State);
	
	conn = null;
		
	// Use the diconnected recordset here.
	
	// Release the recordset.
	rs.Close();
	rs = null;
%>

<SCRIPT LANGUAGE="VBScript" RUNAT="SERVER">
Sub DisconnectRecordset(rs)
	Set rs.ActiveConnection = Nothing
End Sub
</SCRIPT>
				
NOTE: In the preceding code, you cannot replace the following line of code
	DisconnectRecordset(rs);
				
with one of the following lines of code to create a disconnected recordset:
	rs.ActiveConnection = null;
				
-or-
	delete(rs.ActiveConnection);
				
	var oUtil = Server.CreateObject("Torrboy.JArgUtility");
	rs.ActiveConnection = oUtil.Nothing;  
				

References

For additional information, click the article numbers below to view the articles in the Microsoft Knowledge Base:
184397 How To Create ADO Disconnected Recordsets in VBA/C++/Java
190717 INFO: Disconnected Recordsets with ADO or RDS
252482 BUG: ADO Disconnected Recordset That Uses Parameterized Query Is Not Disconnected by SQL Server

Properties

Article ID: 289531 - Last Review: September 6, 2012 - Revision: 2.0
Applies to
  • Microsoft Active Server Pages 4.0
Keywords: 
kbdatabase kbhowto KB289531
Retired KB Content Disclaimer
This article was written about products for which Microsoft no longer offers support. Therefore, this article is offered "as is" and will no longer be updated.

Give Feedback

 

Contact us for more help

Contact us for more help
Connect with Answer Desk for expert help.
Get more support from smallbusiness.support.microsoft.com