Linking ZQuery & TDatasource by code

In this forum you may discuss all issues concerning the Lazarus IDE and Freepascal (both running on Windows or Linux).

Moderators: gto, cipto_kh, EgonHugeist

Post Reply
Miflon
Fresh Boarder
Fresh Boarder
Posts: 2
Joined: 03.12.2019, 13:57

Linking ZQuery & TDatasource by code

Post by Miflon »

Hi everybody.
In a form, i want to create and to link a ZQuery and a TdataSource programmatically. I write that code :

Code: Select all

R: TZQuery;
S: TDataSource;
begin
    R := TZQuery.Create(nil);
    S := TDataSource.Create(nil);
    R.Connection := FicheAccueil.ConnexionPQ; // ConnexionPG is dropped on "FicheAccueil" form
    R.SQL.Text := 'SELECT nor_repertoire FROM s_ent.t_e_norme_nor WHERE nor_id=1;';
    R.Open;
    R.Active:=true;
    S.DataSet:=R.???; // Here is my problem, I don't know what put in there.
    S.Enabled:=true;
end;
When I drop ZQuery and TDataSource on that form and connect them by the Object Inspector, it works well.
Thanks for your help.
Michel.
marsupilami
Platinum Boarder
Platinum Boarder
Posts: 1939
Joined: 17.01.2011, 14:17

Re: Linking ZQuery & TDatasource by code

Post by marsupilami »

Hello Michel,

your line of code would look like this:

Code: Select all

    S.DataSet:=R;
Also calling R.Open and setting R.Active to true is the same - you only need to do one of those.
Best regards,

Jan
Miflon
Fresh Boarder
Fresh Boarder
Posts: 2
Joined: 03.12.2019, 13:57

Re: Linking ZQuery & TDatasource by code

Post by Miflon »

Thanks jan for your advice.
All is working OK now.
Another question about the differences and benefits between creating ZQuery and DataSource by code or dropping these two components on the form?
Michel
marsupilami
Platinum Boarder
Platinum Boarder
Posts: 1939
Joined: 17.01.2011, 14:17

Re: Linking ZQuery & TDatasource by code

Post by marsupilami »

Miflon wrote: 05.12.2019, 11:26 Another question about the differences and benefits between creating ZQuery and DataSource by code or dropping these two components on the form?
Hello,

errm . there is next to no difference - besides you needing to do a lot of work if you do things in code. Things that you put on the form at design time are managed by Lazarus for you. If you do this by hand, you need to do all the management...
User avatar
aehimself
Zeos Dev Team
Zeos Dev Team
Posts: 787
Joined: 18.11.2018, 17:37
Location: Hungary

Re: Linking ZQuery & TDatasource by code

Post by aehimself »

It depends on the context, really. For datasets doing work through the applications lifetime, I'm always using design time. If I need a bit of information from the database (or inserting / editing a record in a specific table) I'm always creating it runtime.

As time passed I was drawn to be Class-y. Meaning, I write a class for everything, doing only one job, publishing only the necessary properties. While it has it's advantages like reusability...

...I'm creating ~85% of my queries in runtime :)
Delphi 12.1, Zeos 8 from latest GIT snapshot
Using:
- MySQL server 8.0.18; libmariadb.dll 3.3.8
- Oracle server 11.2.0, 12.1.0, 19.0.0; oci.dll 21.13
- MSSQL 2012, 2019; sybdb.dll FreeTDS_2435
- SQLite 3.45.2
Fr0sT
Zeos Dev Team
Zeos Dev Team
Posts: 280
Joined: 08.05.2014, 12:08

Re: Linking ZQuery & TDatasource by code

Post by Fr0sT »

I also use short-living query objects for little tasks. I get it with DataModule.GetTempQuery, do what I need and forget about it. As the returned query is wrapped with interface, it is destroyed automatically
User avatar
aehimself
Zeos Dev Team
Zeos Dev Team
Posts: 787
Joined: 18.11.2018, 17:37
Location: Hungary

Re: Linking ZQuery & TDatasource by code

Post by aehimself »

I have a general fear about interfaces and garbage collecting languages (like Java & C#). Even if I create a visual component during runtime, I always create it with .Create(nil) and destroy it by myself when it's not needed anymore. This way I can be sure that it is properly disposed at a time when it SHOULD be disposed.

Don't blame me - I have no trust in things which I cannot see :)
Delphi 12.1, Zeos 8 from latest GIT snapshot
Using:
- MySQL server 8.0.18; libmariadb.dll 3.3.8
- Oracle server 11.2.0, 12.1.0, 19.0.0; oci.dll 21.13
- MSSQL 2012, 2019; sybdb.dll FreeTDS_2435
- SQLite 3.45.2
Fr0sT
Zeos Dev Team
Zeos Dev Team
Posts: 280
Joined: 08.05.2014, 12:08

Re: Linking ZQuery & TDatasource by code

Post by Fr0sT »

The difference between auto-managed types and GC is that with the former you always can tell when resources are freed and in the latter you can't. Auto-managed types could make code significantly shorter and cleaner.
Post Reply