Jump to content
PhilPlus

How to 'disconnect' a TFDQuery keeping the datas.

Recommended Posts

I typically use TFDQuery connected to a database to use with a TDBGrid.
Everything is OK but I would sometimes need to disconnect this TFDQuery while keeping the data in the TDBGrid, just for visualization, with, of course the loss of some functions related to the data update, but it does not pose any problem.
It seems to me that it was possible with the old library 'ADODB' : http://etutorials.org/Programming/ma...ed+Recordsets/
Unfortunately I haven't found how to do this with the Firedac components (apart from making a copy in a TFDMemTable but this is a bit cumbersome).
Has anyone already implemented this principle?

Share this post


Link to post

Hi...:classic_cool:

 

That's why the components are called "data sensitive". Without a direct connection to the database you have no data.... (unless there is something new :classic_tongue:)

You have to come up with a separation of data and database.


Variant 1:
1. load data with query
2. transfer the data into objects
3. throw away query
4. change data in object
5. save object with SQL (create query, throw away query)
... with this variant one gets along also without data-sensitive components. You can then display an object in the grid as well as a property in the edit.

 

Variant 2:

Copy to another dataset:

https://docwiki.embarcadero.com/Libraries/Sydney/en/FireDAC.Comp.DataSet.TFDDataSet.CopyDataSet

 

Imho there are other variants....

 

:classic_smile:

Share this post


Link to post

A TClientDataSet (together with a TDataSetProvider) can be used. Even when the Connection is closed, the TClientDataSet is still showing all data.

This briefcase-model programming style is available since around Delphi 3, where the architecture was branded as MIDAS, later renamed to Datasnap.

 

So basically this is needed:

* a TFDQuery

* which is connected to a TDatasetProvider,  which in turn

* is connected to the TClientDataSet.

 

The TFDQuery or its connection may be closed without losing the data in the TClientDataSet

 

Resources:

https://docwiki.embarcadero.com/Libraries/Sydney/en/Datasnap.Provider.TDataSetProvider

https://docwiki.embarcadero.com/Libraries/Sydney/en/Datasnap.DBClient.TClientDataSet

Edited by mjustin

Share this post


Link to post
12 hours ago, Fr0sT.Brutal said:

Or any memory dataset. I believe Firedac has one

In principle, all FireDAC datasets are memory datasets. TFDMemTable is just a FireDAC dataset with no database connection.

 

FireDAC supports both "offline mode" and cached updates, so there's really no need to use TClientDataSet in this case.

 

However, it sounds as if the OP really just needs to set up the TFDQuery to fetch all data and then close the DB cursor.
Something like this:

Query.FetchOptions.Mode := fmAll;
Query.FetchOptions.AutoClose := True;

 

Share this post


Link to post
On 11/13/2022 at 9:49 AM, haentschman said:

Hi...:classic_cool:

 

That's why the components are called "data sensitive". Without a direct connection to the database you have no data.... (unless there is something new :classic_tongue:)

You have to come up with a separation of data and database.


Variant 1:
1. load data with query
2. transfer the data into objects
3. throw away query
4. change data in object
5. save object with SQL (create query, throw away query)
... with this variant one gets along also without data-sensitive components. You can then display an object in the grid as well as a property in the edit.

 

Variant 2:

Copy to another dataset:

https://docwiki.embarcadero.com/Libraries/Sydney/en/FireDAC.Comp.DataSet.TFDDataSet.CopyDataSet

 

Imho there are other variants....

 

:classic_smile:

Thx I yet use these 2 variants (more  or less) but I would find a simpler way as FDDataset have all the data in memory.

Share this post


Link to post
On 11/13/2022 at 5:42 PM, mjustin said:

A TClientDataSet (together with a TDataSetProvider) can be used. Even when the Connection is closed, the TClientDataSet is still showing all data.

This briefcase-model programming style is available since around Delphi 3, where the architecture was branded as MIDAS, later renamed to Datasnap.

 

So basically this is needed:

* a TFDQuery

* which is connected to a TDatasetProvider,  which in turn

* is connected to the TClientDataSet.

 

The TFDQuery or its connection may be closed without losing the data in the TClientDataSet

 

Resources:

https://docwiki.embarcadero.com/Libraries/Sydney/en/Datasnap.Provider.TDataSetProvider

https://docwiki.embarcadero.com/Libraries/Sydney/en/Datasnap.DBClient.TClientDataSet

Thx, but TClientDataset seems more complex than my (not very cean) solution : a copy to FDMemeryTable, but it may be more efficient (perf and memory).

Share this post


Link to post
10 hours ago, Anders Melander said:

In principle, all FireDAC datasets are memory datasets. TFDMemTable is just a FireDAC dataset with no database connection.

 

FireDAC supports both "offline mode" and cached updates, so there's really no need to use TClientDataSet in this case.

 

However, it sounds as if the OP really just needs to set up the TFDQuery to fetch all data and then close the DB cursor.
Something like this:


Query.FetchOptions.Mode := fmAll;
Query.FetchOptions.AutoClose := True;

 

Ok but even with these options, when the TConnection is closed (FDConnection1.Connected := false) all the data are lost. So how to use the "offlien mode" ?

Share this post


Link to post
37 minutes ago, Anders Melander said:

Read the documentation. Search for FireDAC offline

Excellent Anders, excellent ! I didn't know this, first tests show that it was the exact simple  solution I was looking for.

The code is 

 FDConnection1.Offline;

 

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×