Home arrow Delphi-Kylix arrow Page 4 - Using Delphi with ADO
DELPHI-KYLIX

Using Delphi with ADO


This article, the first of three parts, shows you how to access and change database content using Delphi and ADO. Accessing data both with and without data aware components is covered.

Author Info:
By: Jacques Noah
Rating: 4 stars4 stars4 stars4 stars4 stars / 15
October 02, 2006
TABLE OF CONTENTS:
  1. · Using Delphi with ADO
  2. · Accessing Database Content
  3. · Practical Example with Data Aware Components
  4. · Practical Example Without Data Aware Components

print this article
SEARCH DEVARTICLES

TOOLS YOU CAN USE

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.

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