Home arrow Delphi-Kylix arrow Saving Images and Binary Files to a Database with Delphi 2

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 / 17
July 03, 2007
  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

Saving Images and Binary Files to a Database with Delphi 2
(Page 1 of 5 )

DBImage is compatible with bitmaps only so if you want to save and retrieve other image formats like JPEGs you cannot use the DBImage component. Instead you will have to use the TImage component and add the JPEG unit to the "uses" clause of the unit. Since the TImage component is not data-aware and is not linked to the blob field in the database, we would have to post the image by first loading it to a stream object and then saving the stream data to the database.

With this approach we are trying to save files as raw binary data without worrying about the format to which they belong. Now if we were to use this approach to save JPEGs shouldn't we widen the scope to include other file types too ? Surely, when given an option, the user may want to save GIF, pdf, doc and zip files too.

Saving Binary Files

So let us start with saving binary files to a database.  As you might have already noticed we are saving files to a BLOB (Binary Large Object) field. BLOB fields are fields which store large chunks of data of varying formats and indefinite size as opposed to simple data types.

The BLOB column saves data as binary strings or byte strings and cannot be searched.  The TBlobField class in Delphi can not only contain raw data of arbitrary length but also provides methods to manipulate raw binary data and files. For instance you can copy raw binary data between a BLOB field and a binary file by calling methods of the TBlobField class. One of these methods of the TBlobField class is the "LoadFromFile" method which can be called to save data from a file to the database without much effort. We can modify the example code from the previous article to include this method instead of loading the image to DBImage component.

if OpenDialog1.Execute then

Make sure you have set ADOTable1 to insert mode by using the DBNavigator and after the OpenDialog exits click the "a" (tick) button to save all fields. Navigate the dataset; you will find that the image you inserted last shows up on the DBImage component. I tried saving files other than bitmaps using the same method but it didn't work; nor could I find any information on whether it should. May be I did not search diligently enough but I was not concerned with the issue, since I intended to use streams to save the files and retrieve for reasons I will elucidate later on in this article.

blog comments powered by Disqus

- 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...

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 

Developer Shed Affiliates


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