does opening TMyQuery automatically open TMyConnection?

Discussion of open issues, suggestions and bugs regarding MyDAC (Data Access Components for MySQL) for Delphi, C++Builder, Lazarus (and FPC)
Post Reply
docH
Posts: 59
Joined: Sun 22 Dec 2013 15:18

does opening TMyQuery automatically open TMyConnection?

Post by docH » Fri 15 May 2020 09:17

When running a query using TMyQuery, do I need to explicitly open (and close) the TMyConnection as well as the query or will the query open the connection for me if it is currently closed?
I'd prefer to keep the connection open all the time but just in case I somewhere previously had issued

Code: Select all

MyConnection1.Close;
is it OK to just say

Code: Select all

MyQuery.Close;
MyQuery.SQL.Clear;
MyQuery.SQL.Add(sql);
MyQuery.Open;
...
MyQuery.Close;

or must I say

Code: Select all

MyQuery.Close;
MyQuery.SQL.Clear;
MyQuery.SQL.Add(sql);
MyConnection1.Open;   // <-------------
MyQuery.Open;
...
MyQuery.Close;
MyConnection1.Close;
or even more complicated, if I don't know the current connection state,

Code: Select all

DatabaseConnectionState := MyConnection1.Connected; //remember state
MyQuery.Close;
MyQuery.SQL.Clear;
MyQuery.SQL.Add(sql);
MyConnection1.Open;   // <-------------
MyQuery.Open;
....
MyQuery.Close;
MyConnection1.Connected := DatabaseConnectionState;

ViktorV
Devart Team
Posts: 3168
Joined: Wed 30 Jul 2014 07:16

Re: does opening TMyQuery automatically open TMyConnection?

Post by ViktorV » Fri 15 May 2020 12:41

When you call the method TMyQuery.Open, a connection attempt to the server is made automatically.

docH
Posts: 59
Joined: Sun 22 Dec 2013 15:18

Re: does opening TMyQuery automatically open TMyConnection?

Post by docH » Fri 15 May 2020 15:17

Thanks Viktor. That useful behaviour is very handy to know. I can strip out a lot of unnecessary code now.

ViktorV
Devart Team
Posts: 3168
Joined: Wed 30 Jul 2014 07:16

Re: does opening TMyQuery automatically open TMyConnection?

Post by ViktorV » Tue 19 May 2020 09:32

Thank you for the interest to our product.
If you have any questions during using our products, please don't hesitate to contact us - and we will try to help you solve them.

Post Reply