Page 1 of 1

does opening TMyQuery automatically open TMyConnection?

Posted: Fri 15 May 2020 09:17
by docH
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;

Re: does opening TMyQuery automatically open TMyConnection?

Posted: Fri 15 May 2020 12:41
by ViktorV
When you call the method TMyQuery.Open, a connection attempt to the server is made automatically.

Re: does opening TMyQuery automatically open TMyConnection?

Posted: Fri 15 May 2020 15:17
by docH
Thanks Viktor. That useful behaviour is very handy to know. I can strip out a lot of unnecessary code now.

Re: does opening TMyQuery automatically open TMyConnection?

Posted: Tue 19 May 2020 09:32
by ViktorV
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.