Microsoft KB Archive/289531

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

Article ID: 289531

Article Last Modified on 6/29/2004

-

APPLIES TO


 * Microsoft ActiveX Data Objects 2.0
 * Microsoft ActiveX Data Objects 2.1
 * Microsoft ActiveX Data Objects 2.5
 * Microsoft ActiveX Data Objects 2.6
 * Microsoft Active Server Pages 4.0

-



This article was previously published under Q289531



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=&quot;VBScript&quot;%>

<%   ' Connection string. strCon = &quot;Provider=sqloledb;Data Source=myServer;Initial Catalog=Northwind;User Id=myUser;Password=myPassword&quot; ' Create the required ADO objects. Set conn = Server.CreateObject(&quot;ADODB.Connection&quot;) Set rs = Server.CreateObject(&quot;ADODB.recordset&quot;) ' Open the connection. conn.Open strCon ' Retrieve some records. strSQL = &quot;Select * from Shippers&quot; 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(&quot; Connection.State = &quot; & 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=&quot;JScript&quot;%>

<%   // Connection string. var strCon = &quot;Provider=sqloledb;Data Source=myServer;Initial Catalog=Northwind;User Id=myUser;Password=myPassword&quot;; // Create the required ADO objects. conn = Server.CreateObject(&quot;ADODB.Connection&quot;); rs = Server.CreateObject(&quot;ADODB.recordset&quot;); // Open the connection. conn.Open(strCon); // Retrieve some records. var strSQL = &quot;Select * from Shippers&quot;; 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(&quot; Connection.State = &quot; + conn.State); conn = null; // Use the diconnected recordset here. // Release the recordset. rs.Close; rs = null; %>

 Sub DisconnectRecordset(rs) Set rs.ActiveConnection = Nothing End Sub  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); There is an alternate way to create a disconnected recordset. JScript does not have a keyword similar to the Nothing keyword in VBScript, which is used to release the ActiveX objects. To achieve this, you could use a technique similar to the one given at the following Web site:

http://www.netspace.net.au/~torrboy/code/jargutil

In this case, the sample code might resemble the following: var oUtil = Server.CreateObject(&quot;Torrboy.JArgUtility&quot;); rs.ActiveConnection = oUtil.Nothing;

