How to use the ADO recordset, record and stream objects to open documents


Summary


In Microsoft OLE DB Provider for Internet Publishing, you can open folders and documents into ADO records and ADO recordsets. An ADO record object can represent a row in a recordset, or a single document or folder opened directly. An ADO stream object represents a file in memory. It can be used to display a document that is contained in a record object.

More Information


An ADO record object can represent a row in a recordset, or a single document or folder. The example code in this article describes opening a record from a row in an ADO recordset, and directly on a document. When you click Command1, a document is opened into an ADO recordset. A record object is used to retrieve the Recordset row that contains the document. When you click Command2, a document is opened directly into an ADO record object. In both cases, a stream object is opened on a record. Because a stream is a file in memory, you can perform actions such as displaying the file.

Note To be visible to ADO, the documents must reside in an Internet Information Server's virtual directory. The server should be running Microsoft Windows 2000 or later and Internet Information Server 5.0 or later. The client must have Internet Explorer 5 or later installed.

The following sample uses a document that is named Test.txt in a virtual directory that is named Test. The virtual directory may contain other folders and documents.
  1. In Visual Basic create a new Standard EXE. By default, a form that is named Form1 is created.
  2. Set a Project Reference to the Microsoft ActiveX Data Objects Library.
  3. Add two Command buttons to Form1. By default, Command1 and Command2 are created.
  4. Add a Text box to Form1. By default, Text1 is created. Set the Multiline property of Text1 to True. Set the Scrollbars property of Text1 to Both.
  5. In the Code window, paste the following code in the General Declarations section of Form1:
       Private Sub Command1_Click()

    Dim rs As ADODB.Recordset
    Set rs = New ADODB.Recordset

    Dim rec As ADODB.Record
    Set rec = New ADODB.Record

    Dim stm As ADODB.Stream
    Set stm = New ADODB.Stream

    'Clear the text box
    Text1.Text = ""

    'You must use Serverside cursors with Internet Publishing provider
    rs.CursorLocation = adUseServer

    'Specify adCmdTableDirect when opening a document or folder
    rs.Open "test.txt", "Provider=MSDAIPP.DSO;" & _
    "Data Source=http://localhost/test", _
    adOpenForwardOnly, adLockReadOnly, adCmdTableDirect

    'Read the current row of the Recordset into a Record
    rec.Open rs

    'The RESOURCE_CONTENTCLASS may be used to determine
    ' the document type. In this case, it is text/plain
    Text1.Text = rec.Fields("RESOURCE_CONTENTCLASS").Value

    'An ADO Stream may be Text or Binary
    stm.Type = adTypeText

    'You must specify a Character set to display text
    stm.Charset = "ascii"

    'Read the Record into the stream
    stm.Open rec, adModeRead, adOpenStreamFromRecord

    'Now, read the stream into the text box
    'Use the Steam's ReadText method for text, Read for binary
    Text1.Text = Text1.Text & vbCrLf & stm.ReadText

    stm.Close
    Set stm = Nothing

    rec.Close
    Set rec = Nothing

    rs.Close
    Set rs = Nothing

    End Sub

    Private Sub Command2_Click()

    Dim rec As ADODB.Record
    Set rec = New ADODB.Record

    Dim stm As ADODB.Stream
    Set stm = New ADODB.Stream

    'Clear the text box
    Text1.Text = ""

    'Alternate connection string.
    'If the provider is specified, you cannot use the URL syntax.
    'If URL syntax is used, MSDAIPP.DSO is assumed, and
    ' provider cannot be specified
    rec.Open "test.txt", "URL=http://localhost/test", , , _
    adCmdTableDirect

    stm.Type = adTypeText
    stm.Charset = "ascii"
    stm.Open rec, adModeRead, adOpenStreamFromRecord

    Text1.Text = stm.ReadText

    stm.Close
    Set stm = Nothing

    rec.Close
    Set rec = Nothing

    End Sub

References


For a more advanced sample that uses ADO records and streams to work with folders and documents, visit the following Microsoft Developer Network Web site: The Internet Publishing Scenario is also available in the MSDN Library. In the MSDN Library Index, search for the following:
Internet Publishing scenario(ADO). 
For additional information about connection strings that open a document into an ADO recordset, click the following article number to view the article in the Microsoft Knowledge Base:

245359 How to open documents by using the Internet Publishing Provider

For more infomation about Microsoft ActiveX Data Objects, visit the following MSDN Web site: