Title: Replaced by art. 2845 'Simple guide to ADO' by the author
Question: I article #1489 I gave my idea about using the ADO connect component; in article #1490 I gave my idea of using the ADO command component. But how to use that ADO command component in more complex situations?
Answer:
In my previous article I described the way to use the ADO command with debug possiblities.
An example of using the the ADO command component that way is:
procedure X;
var
currentkey, commtext: string;
begin
..
..
currentkey := dataform.adoquery1.fieldbyname('system_key').asstring;
commtext := 'delete from table1 where system_key = ' + currentkey ;
dataunit.exec_command(commtext); {see my previous article}
..
..
end;
No problem so far.
Assume
- we want to insert a record,
- the key is generated by the database,
- we want to enter:
an integer value for the field 'integer_field' and
a string for the field 'string_field'
procedure Y;
var
inputstring, commtext: string;
inputinteger : integer ;
begin
..
..
inputinteger := 1 ;
inputstring := 'Hello world' ;
commtext := 'insert into table1 (integer_field, string_field) ' +
'values (' +
inttostr(inputinteger) + ',' +
'"' + inputstring + '"' + ')' ;
dataunit.exec_command(commtext); { see my previous article }
..
..
end;
This starts to become fussy. So is there a simpler way? Yes!
procedure Z;
var
inputstring, commtext: string;
inputinteger : integer ;
begin
..
..
inputinteger := 1 ;
inputstring := 'Hello world' ;
commtext := 'insert into table1 (integer_field, string_field) ' +
'values ( :inputinteger, :inputstring )' ;
dataunit.ado_comm_1.commandtext := commtext ;
dataunit.ado_comm_1.parameters.parsesql(commtext,true);
dataunit.ado_comm_1.parameters[0].value := inputinteger ;
dataunit.ado_comm_1.parameters[1].value := inputstring ;
dataunit.ado_comm_1.execute;
..
..
end;
Remark: Procedure Z is a bit more code, but:
- when the insert is within a loop, only the last three lines of
code are in the loop.
- real inserts are far more complex with a lot of field from all
types and the commandstring becomes very complex.
The debug facilities of my previous article are lost, but without regret: the problems described in my previous article alway came from the composing of the more complex sql-strings, which were difficult to debug in the Delphi5 debugger.