Learn How To Easily Work With BLOB Streams Internally And Externally In This Delphi Windows App

The BlobStreams sample shows you how to use the BLOB streaming techniques of FireDAC. To this end, this sample implements both the external streaming and the internal streaming.

Location

You can find the BlobStreams sample project at:

  • Start | Programs | Embarcadero RAD Studio Sydney | Samples and then navigate to:
    • Object PascalDatabaseFireDACSamplesComp LayerTFDQueryBlobStreams
  • Subversion Repository:
    • You can find Delphi code samples in GitHub Repositories. Search by name into the samples repositories according to your RAD Studio version.
blobstreams

How to Use the Sample

  1. Navigate to the location given above and open BlobStreams.dproj.
  2. Press F9 or choose Run > Run.

Files

File in Delphi Contains
BlobStreams.dproj
BlobStreams.dpr
The project itself.
fBlobStr.pas
fBlobStr.fmx
The main form.

Implementation

Before running the sample, the main components are configured at design time using the Object Inspector as follows:

  • Two TFDQuery objects named qSelect and qInsert. These components are used to implement the datasets capable of executing SQL queries. The following setup is needed:
  • Configure the Connection property of both objects to specify the FireDAC connection object that is used to connect to a DBMS. In both cases it is set to FDConnection1.
  • Set the SQL property for both objects. It specifies the SQL statement to execute for the query.
    • The SQL property of qSelect is set to select * from {id FDQA_Blob}.
    • The SQL property of qSelect is set to insert into {id FDQA_Blob} (blobdata) values (:blobdata).
  • Configure the qInsert.Param[0]:
    • The Name property is se to BLOBDATA
    • The DataType property is set to one of the BLOB data types. In this sample ftBlob.
    • The ParamType property is set to ptInput. It means that the stream will be read and written to a database BLOB value.
    • The StreamMode property is set to smOpenRead. This is the default value. It means that the stream is used to read the database BLOB value.
  • TDataSource object named DataSource1. This component provides an interface between a dataset component and data-aware controls on a form. In this sample, it is used to provide communication between the dataset and a TDBMemo object named DBMemo1DBMemo1 is used to display the blobdata field of the dataset. To this end, the following setup is needed:
  • The DataSet property of DataSource is set to qSelect.
  • The DataSource property of DBMemo1 to DataSource1.
  • The DataField property of DBMemo1 to blobdata.

Once the main components are configured at design time you can run the application and interact with the sample in run time. To this end, the sample offers three buttons:

  • Prepare data. This button creates an OnClick event that uses the TFileStream.Create method to instantiate the file stream in order to write a string into the file.
  • Insert (external stream). This button creates an OnClick event that implements an external streaming. An external stream is provided by the application to FireDAC (external to FireDAC). Then, FireDAC reads this stream.

See the code below:

  • Insert (internal stream). This button creates an OnClick event that implements an internal streaming. An internal stream is provided by FireDAC to the application (internal to FireDAC). Then, the application reads this stream. To this end, the sample does the following:
    • Use the StarTransaction method to start a mandatory transaction.
    • Set the parameter DataType to ftStream.
    • Set the parameter StreamMode to smOpenWrite in order to write the database BLOB value.
    • Use the ExecSQL method to execute the SQL command. This will return the internal stream reference.
    • Write the internal stream reference.
    • Call the CloseStreams method to flush database API buffers and close internal streams.
    • Use the Commint method to finish the transaction.

For more information about this sample, please refer to the link below:
http://docwiki.embarcadero.com/CodeExamples/Sydney/en/FireDAC.TFDQuery.BlobStreams_Sample

Check out the full source code for the BLOB streams sample app in Windows for Delphi on GitHub.