Delphi-Kylix
  Home arrow Delphi-Kylix arrow Page 2 - Saving Images and Binary Files to a Databa...
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
DELPHI-KYLIX

Saving Images and Binary Files to a Database with Delphi 2
By: Danish Ahmed
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 5
    2007-07-03

    Table of Contents:
  • Saving Images and Binary Files to a Database with Delphi 2
  • Using Streams to save binaries
  • Retrieving BLOB data and saving to file
  • Checking the First 4 Bytes or Magic Numbers
  • Code

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    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;

    More Delphi-Kylix Articles
    More By Danish Ahmed


     

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







    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 6 Hosted by Hostway
    Stay green...Green IT