Streams and Persistence

Microsoft ActiveX Data Objects (ADO)

Streams and Persistence

The Recordset object Save method stores, or persists, a Recordset in a file, and the Open method restores the Recordset from that file.

Now, the Save and Open methods can persist a Recordset to a Stream object as well. This feature is especially useful when working with Remote Data Service (RDS) and Active Server Pages (ASP).

See the current ASP documentation for more information about how persistence can be used by itself on ASP pages.

The following are a few scenarios that show how Stream objects and persistence can be used.

Scenario 1:

This scenario simply saves a Recordset to a file, then to a Stream. It then opens the persisted stream into another Recordset.

Dim rs1 As ADODB.Recordset
Dim rs2 As ADODB.Recordset
Dim stm As ADODB.Stream

rs1.Open    "SELECT * FROM Authors", "DSN=Pubs;uid=sa;pwd=;", _
            adopenStatic, adLockReadOnly, adCmdText
rs1.Save "c:\myfolder\mysubfolder\myrs.xml", adPersistXML
rs1.Save stm, adPersistXML
rs2.Open stm

Scenario 2:

This scenario persists a Recordset into a Stream in XML format. It then reads the Stream into a string that you can examine, manipulate, or display.

Dim rs As New ADODB.Recordset
Dim stm As New ADODB.Stream
Dim strRst As String

' Open, save, and close the recordset. 
rs.Open "select * from Authors", "dsn=Pubs;uid=sa;pwd=;"
rs.Save stm, adPersistXML
rs.Close
Set rs = nothing

' Put saved recordset into a string variable.
strRst = stm.ReadText(adReadAll)

' Examine, manipulate, or display the XML data.
...

Scenario 3:

This example code shows ASP code persisting a Recordset as XML directly to the Response object.

...
<%
response.ContentType = "text/xml"

' Create and open a recordset.
Set rs = Server.CreateObject("ADODB.Recordset")
rs.Open "select * from Authors", "dsn=Pubs;uid=sa;pwd=;"

' Save recordset directly into output stream.
rs.Save Response, adPersistXML 

' Close recordset
rs.Close
Set rs = nothing
%>
...

Scenario 4:

In this scenario, ASP code writes the contents of the Recordset in ADTG format to the client. The Cursor Service can use this data to create a disconnected Recordset.

A new property on the RDS DataControl, URL, points to the .asp page that generates the Recordset. This means a Recordset object can be obtained without RDS using the server-side DataFactory object or the user writing a business object. This simplifies the RDS programming model significantly.

Server-side code named, http://server/directory/recordset.asp:

<%
Dim rs 
Set rs = Server.CreateObject("ADODB.Recordset")
rs.Open "select au_fname, au_lname, phone from Authors", "dsn=Pubs"
response.ContentType = "multipart/mixed"
rs.Save response, adPersistADTG
%>

Client-side code:

<HTML>
<HEAD>
<TITLE>RDS Query Page</TITLE>
</HEAD>
<body>
<CENTER>
<H1>Remote Data Service 2.5</H1>
<TABLE DATASRC="#DC1">
    <TR> 
        <TD><SPAN DATAFLD="au_fname"></SPAN></TD>
        <TD><SPAN DATAFLD="au_lname"></SPAN></TD>
        <TD><SPAN DATAFLD="phone"></SPAN></TD>
    </TR>
</TABLE>
<BR>

<OBJECT classid="clsid:BD96C556-65A3-11D0-983A-00C04FC29E33"
    ID=DC1 HEIGHT=1 WIDTH = 1>
    <PARAM NAME="URL" VALUE="http://server/directory/recordset.asp">    
</OBJECT>

</SCRIPT>
</BODY>
</HTML>

Developers also have the option of using a Recordset object on the client.

...
function GetRs() 
    {
    rs = CreateObject("ADODB.Recordset");
    rs.Open "http://server/directory/recordset.asp"
    DC1.SourceRecordset = rs;
    }
...