What is the best way to do this in Delphi?

I need to create a adoquery in execution time.

what is the best way to create, with application or nil in create?

var
  TQ : Tadoquery;
begin
  TQ := Tadoquery.create(Application)

  OR

  TQ := Tadoquery.create(Nil)
end;



End to destoy it?

TQ.Close;
FreeAndNil(TQ)

Or

TQ.free


Thanks
LVL 1
hidrauAsked:
Who is Participating?
 
Emmanuel PASQUIERFreelance Project ManagerCommented:
Usually you create components with a FORM owner. The Owner MUST be a TComponent, which is the case of TApplication.
The component (ADOQuery) will be destroyed automatically when the owner is destroyed.
If you set no Owner (nil), then you will HAVE to destroy it manually.

But there is no best way to create dynamically the component, it depends on how much control on your object life you need/want,how much public is that ADO object (used by only one form or two ? or always available in all your forms and units ?) and where you create/destroy your object.

Then, that said, the best is to not create dynamically those objects, but put one at design time in a data module. Let Delphi manage automatically the datamodule and its owned components (the ADOQuery will be one of them).
0
 
hidrauAuthor Commented:
ok, I understood - thanks
0
 
hidrauAuthor Commented:
thanks
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.