ADO Stream Object

[Previous] [Next]

In addition to the Record object, ADO 2.5 introduces the Stream object to the ADO object model. Although you can use the Stream object in conjunction with the Record object to access document-based data, the Stream object can also be used independently in ways that traditional database programmers and web programmers alike will find useful.

Working with Document Data

While the Record object allows you to interact with the structure of documents, the Stream object lets you access the contents of those documents. You can use the Stream object's Open method to access the default stream of data associated with the Record. From there, you can read and modify the contents of the document through the Stream object's properties and methods. The code that follows uses a Stream object to store the contents of a file in a Record object:

 Stream.Open Record, adModeReadWrite, adOpenStreamFromRecord Stream.Position = 0 Stream.LoadFromFile strPathToFile Stream.Flush 

Working with Persistent Data

In ADO 2.5, the Recordset object's persistence features have been enhanced to work with Stream objects. You can persist data to a Stream rather than to a file by using the Stream object's Save method. To turn that data back into a Recordset object, simply use the Stream object as the Source parameter on the Recordset's Open method.

Working with BLOB Data

Writing code to access and modify BLOB (binary large object) data (long string and binary fields) in your database has never been easy. Programmers using ADO and its predecessors have had to rely on the Field object's GetChunk and AppendChunk methods…until now.

BLOB fields often contain the contents of files. The Stream object greatly simplifies the process of interacting with this data. You don't need to access the data in small chunks or determine how much data you're about to access. For example, to retrieve the contents of the ImageField field and store that data in a file, you could use the following code:

 Stream.Type = adTypeBinary Stream.Write Recordset.Fields("ImageField").Value Stream.SaveToFile strPathToFile 

To move the contents of the file back to the database, you'd use this code:

 Stream.Type = adTypeBinary Stream.LoadFromFile strPathToFile Recordset.Fields("ImageField").Value = Stream.Read 



Programming ADO
Programming MicrosoftВ® ADO.NET 2.0 Core Reference
ISBN: B002ECEFQM
EAN: N/A
Year: 2000
Pages: 131
Authors: David Sceppa

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net