FireDAC.TFDQuery.BlobStreams Sample
This sample demonstrates the two kinds of BLOB streaming that FireDAC offers: External streaming and Internal streaming.
Contents
Location
You can find the BlobStreams sample project at:
- Start | Programs | Embarcadero RAD Studio Athens | Samples and then navigate to:
Object Pascal\Database\FireDAC\Samples\Comp Layer\TFDQuery\BlobStreams
- Subversion Repository:
- You can find Delphi code samples in GitHub Repositories. Search by name into the samples repositories according to your RAD Studio version.
Description
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.
How to Use the Sample
- Navigate to the location given above and open
BlobStreams.dproj
. - Press F9 or choose Run > Run.
Files
File in Delphi | Contains |
---|---|
|
The project itself. |
|
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.
- 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.
- The Name property is se to
- 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
- A 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 DBMemo1. DBMemo1 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
.
- The DataSet property of DataSource is set to
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. To this end, the sample does the following:
- See the code below:
procedure TfrmBlobStr.btnInsertExternalClick(Sender: TObject); begin // ... qInsert.SQL.Text := 'insert into {id FDQA_Blob} (blobdata) values (:blobdata)'; // Set parameter data type to one of the BLOB types. qInsert.Params[0].DataType := ftOraBlob; // Assign external stream before ExecSQL. FireDAC takes ownership of the stream object qInsert.Params[0].AsStream := TFileStream.Create(C_File, fmOpenRead); qInsert.ExecSQL; // ...
- 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.
- See the code below:
procedure TfrmBlobStr.btnInsertInternalClick(Sender: TObject); begin FDConnection1.StartTransaction; // ... qInsert.SQL.Text := 'insert into {id FDQA_Blob} (blobdata) values (:blobdata)'; // Set parameter data type ftStream and do not assign stream reference. qInsert.Params[0].DataType := ftOraBlob; qInsert.Params[0].StreamMode := smOpenWrite; qInsert.ExecSQL; oFS := TFileStream.Create(C_File, fmOpenRead); try // Write to internal stream. The stream is available after ExecSQL. qInsert.Params[0].AsStream.CopyFrom(oFS, -1); finally oFS.Free; end; // Flush/close the streams. qInsert.CloseStreams; FDConnection1.Commit; // ...
Uses
See Also
- FireDAC
- Defining Connection (FireDAC)
- Working with DataSets (FireDAC)
- Navigating and Manipulating Records
- Support for BLOB streaming in FireDAC
Samples
- FireDAC TFDQuery Blobs sample