HOWTO: Use the ADO Recordset, Record and Stream Objects to Open Documents
ID: Q248255
|
The information in this article applies to:
-
Microsoft Data Access Components version 2.5
-
ActiveX Data Objects (ADO), version 2.5
-
Microsoft OLE DB
SUMMARY
The Microsoft OLE DB Provider for Internet Publishing allows you to 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 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 below shows opening a record from a row in an ADO recordset, and directly on a document. When Command1 is clicked, a document is opened into an ADO recordset. A record object is used to retrieve the Recordset row containing the document. And when Command2 is clicked, a document is opened directly into an ADO record object. In both cases, a stream object is opened on a record. Since a stream is a file in memory, we can perform actions such as displaying the file.
Note that in order to be visible to ADO, the documents must reside in an Internet Information Server's virtual directory. The server should be running Internet Information Server 5.0 or later, on a Windows 2000 system or later. The client must have Internet Explorer 5 or later installed.
The following sample uses a document named Test.txt in a Virtual Directory named Test. The Virtual Directory may contain other folders and documents.
- In Visual Basic create a new Standard EXE. Form1 is created by default.
- Set a Project Reference to the Microsoft ActiveX Data Objects Library.
- Add two Command buttons to Form1. Command1 and Command2 are created by default.
- Add a Text box to Form1. Text1 is created by default. Set the Multiline property of Text1 to True. Set the Scrollbars property of Text1 to Both.
- Paste the following code into the General Declarations section of Form1's Code Window:
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
© Microsoft Corporation 1999, All Rights Reserved. Contributions by John Hamrick, Microsoft Corporation
REFERENCES
For a more advanced sample that uses ADO records and streams to work with folders and documents, please visit the following site:
Internet Publishing Scenario
The Internet Publishing Scenario is also available in the MSDN Library. In the MSDN Library Index, search on Internet Publishing scenario(ADO).
For complete documentation on the Microsoft ActiveX Data Objects, please visit the following site on the World Wide Web:
Microsoft ActiveX Data Objects
Additional query words:
Keywords : kbMDAC kbOLEDB kbProvider kbVBp600 kbGrpVBDB kbGrpMDAC kbDSupport kbMDAC250
Version : WINDOWS:2.5
Platform : WINDOWS
Issue type : kbhowto
|