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