Saving Images and Binary Files to a Database with Delphi 2 - Retrieving BLOB data and saving to file
(Page 3 of 5 )
So we have now successfully saved the file to the database but how do we retrieve it? You can use the TADOTable component itself to find the field or query for the record through the TADOQuery component by executing a SQL statement. However, I will introduce the TADODataSet component which can retrieve records directly from a table or from multiple tables through a SQL statement. You can connect to the server by specifying the connection parameters in its "ConnectionString" property but as I said earlier it is always advisable to connect through a single connection component. In my last article we used a TADOConnection component and created the connection string to connect to the server. Add the TADODataSet component to the form (available in the palette "dbGo") and set its 'connection' property to ADOConnection1.

We can utilize the CommandText property of ADODataSet1 to execute a SQL statement and access data in the returned recordset by accessing the component's properties and methods. The data in the blob field can be saved to any file. If you have a column with information on the type of file when it was saved as blob data, you can use the information to save the data to the same file type again. Here is a sample code to fetch a recordset matching the value assigned to the variable iPKey :
ADODataSet1.Connection:=ADOConnection1;
ADODataSet1.CommandText:='Select * From ImgTbl Where PKey=:param0';
ADODataSet1.Parameters.ParamByName('param0').Value:=iPKey;
ADODataSet1.Open;
TBlobField(ADODataSet1 .FieldByNam('Image')).SaveToFile
('Img'+IntToStr(iPKey)+'.dat');
Next: Checking the First 4 Bytes or Magic Numbers >>
More Delphi-Kylix Articles
More By Danish Ahmed