Delphi-Kylix
  Home arrow Delphi-Kylix arrow Page 4 - Using Delphi with ADO
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

Using Delphi with ADO
By: Jacques Noah
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 6
    2006-10-02

    Table of Contents:
  • Using Delphi with ADO
  • Accessing Database Content
  • Practical Example with Data Aware Components
  • Practical Example Without Data Aware Components

  • 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


    Using Delphi with ADO - Practical Example Without Data Aware Components


    (Page 4 of 4 )

    The only differences between an application that uses data aware components and one that does not are the amount of memory used and the loss of flexibility in code. When you create a program without data aware components, the application will be more dynamic in terms of using multiple tables, manipulating records, and so forth. The only downside is that you need to write code, as opposed to not writing any code when you use data aware components. But even the amount of code that you write will be little, depending on what kind of program you are creating.

    This is what I do when creating a database application without data aware components:

    Set the database path

    I usually set the connection details in the form's "Oncreate" event:

    procedure TForm1.FormCreate(Sender: TObject);
    begin
    ado1.connectionstring:='Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:Magazine TutorialsDelphi-ADOpeople.mdb;Persist Security Info=False';
    end;

    At this point you've only set the path to the database. Next you need to set the table that you want to use. This is just as easy, and can be set in any part of your program code, depending on need:

    Set/change the table name:

    Ado1.close; //close the open table first

    //change the table name
    Ado1.Tablename:=tablename.text; //use tedit here...

    //open the new table
    Ado1.open;

    Why a Tedit? Well, because this way you can change the table name easily at run time. Alternatively, you can also use a variable that will hold the name of the table and change as needed.

    To add new data to an ADOTable programmatically

    //put table in insert mode
    ado1.Insert;

    //add the new data, in this case the name and age fields get new values.
    ado1.FieldByName('name').Text
    ado1.FieldByName('age').AsInteger:=45;

    ...

    //Finally, post the new data to the table..
    ado1.Post;

    To programmatically put a table in insert mode you write:

    Ado1.insert;

    To add new data to a field in a table, write:

    ado1.FieldByName(fieldname).Text:= newdata;

    Searching for a records in a table:

    ado1.Locate(fieldname,thevaluetosearchfor,[]);

    OR if you search in multiple fields:

    ado1.Locate(fieldname1, fieldname2,VarArray(value1,value2),[]);

    For example, if we wanted to search for "John Doe" in our table, this is how we'd do it:

    procedure TForm1.Button1Click(Sender: TObject);

    begin
      if not ado1.Locate('name;surname',VarArrayOf(['John','Smith']),[]) then
        begin
          showmessage('Could not find the record that you are looking for');
        end;
    end;

    Conclusion

    As you can see, it is not difficult to write a database application, programmatically. It is even easier to create a database application when you use database aware components. When deploying your Microsoft Access ADO-based application, make sure that you include code that will check to see whether the target machine has MDAC installed. It is probably better to use a commercial installation program such as Install Shield or others that will do all the checking that you'll need to successfully deploy your application. Next, we will discuss the use of the ADOQuery component to run queries on the person table.


    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.

       · the database "people.mdb" is missing from the download. Any chance of having the...
       · OK the MDB file is in the zip now but.... there are no tables in the database???
       · I'll have a look at the file and upload a proper one. Thanks.
       · it seems that mister Jacques Noah forgot about the common people in his quest for...
     

    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 2 hosted by Hostway