Link to home
Start Free TrialLog in
Avatar of Marius0188
Marius0188

asked on

Delphi 7 :: MS SQL 2005 :: INSERT SQL Statement & Params

Hi Experts,

I am building a Delphi 7 app using MS SQL 2005 Express db.
Can you please show me how a correct INSERT, UPDATE and SELECT statement should look if
using ADO components?

I know the SQL syntax, more interested in whether one should use transactions etc?
Please show me complete Delphi code example when inserting a new row using ParamByName() etc...

All advice welcome.

Thank you
Avatar of Marius0188
Marius0188

ASKER

What I would like to do is exactly as when using TIBQuery component:


IBQuery.SQL.Text := 'INSERT INTO TBLTABLE1 (Name, Surname, BirthDate) VALUES(:Name, :Surname, :BirthDate)';
IBQuery.ParamByName('Name').AsString := 'Marius';
IBQuery.ParamByName('Surname').AsString := 'NA';
IBQuery.ParamByName('BirthDate').AsDateTime := Now;
IBQuery.ExecSQL;

With ADO Query components you don't have "ParamByName().AsDateTime" etc...
How do you insert a date etc type?

And then please remember to answer whether your should use transactions etc...

Thanks!
ASKER CERTIFIED SOLUTION
Avatar of imitchie
imitchie
Flag of New Zealand image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
you don't really have to specify parameter types. they are usually self discovered.  as with tdatabase, tibdatabase (tibconnection?), the transaction is managed at the tadoconnection level.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Can one please show me an example of starting and ending a transaction with MS SQL 2005?

Thanks a lot!

ADOQuery.Connection.BeginTrans;
try
  ADOQuery.SQL.Text := 'INSERT INTO TBLTABLE1 (Name, Surname, BirthDate) VALUES(:Name, :Surname, :BirthDate)';
  ADOQuery.Parameters.ParamByName('Name').Value := 'Marius';
  ADOQuery.Parameters.ParamByName('Surname').Value := 'NA';
  ADOQuery.Parameters.ParamByName('BirthDate').Value := Now;
  ADOQuery.ExecSQL; 
  ADOquery.Connection.CommitTrans;
except
  ADOquery.Connection.RollbackTrans;
  raise;
end;

Open in new window