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  
Dedicated Servers  
Actuate Whitepapers 
VeriSign Whitepapers 
IBM® developerWorks 
Sun Developer Network 
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 / 3
    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
     
    Iron Speed
     
    ADVERTISEMENT

    Generate data entry and reporting .NET Web apps in minutes, straight from your database. Read our FREE whitepaper “Build Web 2.0 Applications Without Hand-Coding” Download now!

    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

    - Client Dataset: Working with Data Packets an...
    - Using the Client Dataset in an N-Tiered Appl...
    - Using the Client Dataset in Two-Tiered Clien...
    - Using the Client Dataset in File-Based Archi...
    - Demystifying the Client Dataset
    - Working with INI Files in Delphi
    - Creating Data Link (UDL) Files in Delphi
    - Looking at the Details for an Invoicing Appl...
    - Invoicing in Delphi: Show Me the Money
    - Saving Images and Binary Files to a Database...
    - Saving Files to a Database using Delphi: Sav...
    - Creating CF Applications and Integrating a S...
    - Using Try and Finally to Help Prevent Memory...
    - The Implementation of an FTP Server
    - FTP Server: The Theory


    Iron Speed





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway