Home arrow Delphi-Kylix arrow Page 2 - 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 - Using Streams to save binaries
(Page 2 of 5 )

Streams are classes that  can be used to read from and write to different storage media like memory, files, blobs and sockets. All stream objects in Delphi are descendants of the abstract TStream  class and inherit most of its methods and properties, but each stream can deal with only a single type of media. For instance the TfileStream can be used to read and write to files only.

So we will create a TFileStream object to temporarily store data before it is saved to database.  Now let's get to the coding part. On the ButtonClick event of Button1 we will execute the OpenDlg to let the user select a file to upload, and this time we will set the filter of the dialog box to accept all types of files. Then we create the stream object passing the selected file as the parameter so that the file is now loaded into the FileStream. 

TBlobField has a member method named "LoadFromStream" which can be used to save data from the stream to the blob field. Call the "Post" method and we are done. The selected file is now saved in the database as a Binary Large Object (Blob). If you have SQL Enterprise Manager you can execute a simple SQL statement in the SQL Query Analyzer to see the raw data in the grid.

Here is the code that saves the file to database. 

var
 fStream:TFileStream;  

 begin
  OpenDialog1.Filter:='All Files(*.*)|*.*';
  if OpenDialog1.Execute then
   
fStream:=TFileStream.Create(OpenDialog1.FileName,fmOpenRead);
   
ADOTable1.Insert;
 
    try
      
TBlobField(ADOTable1.FieldByName('image')).LoadFromStream(fStream);
    
finally
      
fStream.Free;
    end;   
  ADOTable1.Post;
 end;


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 8 - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials