Home arrow Delphi-Kylix arrow Page 3 - Saving Images and Binary Files to a Database with Delphi 2
DELPHI-KYLIX

Saving Images and Binary Files to a Database with Delphi 2


In my previous article I talked about ADO Components and how to use them to conduct database transactions from our application. We created a small application to save bitmap files to a database and retrieve them using the data-aware TDBImage component. In this article you'll learn how to save other types of files.

Author Info:
By: Danish Ahmed
Rating: 5 stars5 stars5 stars5 stars5 stars / 8
July 03, 2007
TABLE OF CONTENTS:
  1. · Saving Images and Binary Files to a Database with Delphi 2
  2. · Using Streams to save binaries
  3. · Retrieving BLOB data and saving to file
  4. · Checking the First 4 Bytes or Magic Numbers
  5. · Code

print this article
SEARCH DEVARTICLES

TOOLS YOU CAN USE

advertisement
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');


blog comments powered by Disqus
DELPHI-KYLIX ARTICLES

- Loading an XML Document into the DOM
- Delphi Wrapper Classes and XML
- Delphi and the DOM
- Delphi and XML
- Internet Access: Client Service
- Finishing the Client for an Internet Access ...
- The Client for an Internet Access Control Ap...
- User Management for an Internet Access Contr...
- Important Procedures for an Internet Access ...
- Server Code for an Internet Access Control A...
- Constructing the Interface for an Internet A...
- Building a Server Application for an Interne...
- Building an Internet Access Control Applicat...
- Client Dataset: Working with Data Packets an...
- Using the Client Dataset in an N-Tiered Appl...

Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 



© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 9 - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials