Delphi-Kylix
  Home arrow Delphi-Kylix arrow Page 4 - Saving Files to a Database using Delphi: S...
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  
Moblin 
JMSL Numerical Library 
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 Files to a Database using Delphi: Saving Bitmaps to a Database
By: Danish Ahmed
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 6
    2007-06-26

    Table of Contents:
  • Saving Files to a Database using Delphi: Saving Bitmaps to a Database
  • Create a new project
  • More components
  • Creating the application

  • 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 Files to a Database using Delphi: Saving Bitmaps to a Database - Creating the application


    (Page 4 of 4 )

     

    Now that we are a bit wiser as to what these components do, let us go through the simple procedure of creating a database application to save bitmap files to the database. Assuming that Delphi is up and running, let us switch to the designer mode. Select the ADOConnection1 component, right-click on it and click "Edit ConnectionString" from the pop-up menu or click the ellipses in the "ConnectionString" property in the Object Inspector.

    The dialog that is presented to us contains two options. The first option allows you to specify the datalink (.udl) file and the second option lets us build a new Connection String. It is obvious that we would select the second option since we are creating this connection for the first time. Clicking the "build" button brings up the ConnectionString Editor. Select "OLE DB Provider for SQL Server" from the list of providers and click next. Select the server name from the drop down box, enter username, password and select the database from the drop down box. Click "Test Connection" to find out whether or not you are able to connect to the database. Click OK to close the ConnectionString Editor.

    Next select ADOTable1 component. From the Object Inspector set its "Connection" property to ADOConnection1 and from the drop-down box in the TableName field, select the table "imgTbl." After that, set the dataset property of the Datasource1 component to ADOTable1 and make sure that the active property of the table is set to true. Select DBEdit1 and in the Object Inspector you can set its Data Source property to reference DataSource1; the drop-down box for field property lists all the available fields in the table. Select the field ImageDescription since we want to display the description in the DBEdit1 component.

    Repeat the same procedure with DBImage1 except for the field property which should now point to the blob field of the database. Set DBNavigator1's datasource property to Datasource1. In the design mode itself you can now view the image in the database on the DBImage1 component if there are any images in the database. If you are unable to view it then make sure the Connection object is connected and ADOTable1's active properties are set to true.

    The changes we made above would look something like the code below if we were to do it programmatically. In fact, you can place it within an event to set the properties at runtime:

    ADOConnection1.LoginPrompt:=False;

    ADOTable1.Connection:=ADOConnection1;
    ADOTable1.TableName:='ImagesD';
    DataSource1.DataSet:=ADOTable1;
    DBEdit1.DataSource:=DataSource1;
    DBEdit1.DataField:='ImgDesc';
    DBImage1.DataSource:=DataSource1;
    DBImage1.DataField:='Image';
    DBNavigator1.DataSource:=DataSource1;
    ADOConnection1.Open;
    ADOTable1.Open;

    Before we proceed further we need to add a method to select and load the picture which can later be posted to the database during run-time. Double-clicking on Button1 takes you to the code editor. Set the filter property of the Dialog box to ".bmp" so that it lists only bitmaps, and then execute Open Dialog1 to select the file you want to save. You can then load the selected image file to the DBImage component.

    begin
      OpenDialog1.Filter:='*.bmp|*.bmp';
      if OpenDialog1.Execute then
        begin
          DBImage1.Picture.LoadFromFile(OpenDialog1.Filename);
        end;
    end;

    The above code allows you to select a .bmp file and load it on the DBImage component which is bound to the image field in the database. However, the image is not automatically saved to the database; you need to call the Post method to save it. Further, the dataset should be in Insert or Edit mode, so make sure that you call the dataset's "insert" or "edit" method before calling the "Post" method. But you don't need to worry about that part if you are using the TDBNavigator component. The DBNavigator provides an easy interface for navigating the dataset and inserting, editing, deleting records without writing a single line of code. All that needs to be done is that the DBNavigator's datasource property should be set to point to the datasource component which is linked to the active dataset.

    Once you have ensured that the properties of all the components are set correctly, run the application. Move your cursor over the navigation bar to know what each button does if you are not familiar with the icons on the bar. Click the plus (+) button on the DBNavigator1 to insert a new record, then click on the button captioned " LoadImage." The open dialog pops up; select a bitmap image and click OK. The selected image is now visible on the DBImage1 component; enter some description in the DBEdit1 and click on the button with the tick (a) on it on DBNavigator1 to save the data to the database.

    Retrieving the data is no big deal either. The components are linked to the fields in the database, so as you navigate through records the data-aware controls like DBEdit and DBImage update their values to display the data in the current record of the dataset.

    We have created a fully functional database application that saves and retrieves images from the database, and we didn't have to code the entire thing. In fact the small amount of code that we added was to load the image on the component and was not related to database operation. However, the TDBImage component supports bitmaps only, which makes it impossible to work with other formats like JPEG and GIF using this approach and that is not a minor disadvantage. In the follow up article I will discuss how to save not only other images but also any other file to the database by using stream and how to recognize the format while retrieving data.

    Danish Ahmed

    Mindfire Solutions


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

     

    DELPHI-KYLIX ARTICLES

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







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