Opening and Closing the datasets in Delphi

11,315

Solution 1

Use either as here is little difference, the DataSet.Open method has one line of code: Active := True. Active being a property that will call SetActive or GetActive. Finding out these things is fairly straight forward if you hold down CTRL and click on Open or Active and have a read of the code in the VCL source, knowing a few of the internals of the VCL will stop you doing things like:

if not DataSet.Active then
  DataSet.Open;

rather than just

DataSet.Open;

As TLama indicated, Active is a published property making it available to the Delphi IDE, allowing you to toggle it at design time for your DataSets on Forms or Datamodules. Open and Close are probably not strictly required, but is a fairly common pattern across many languages.

Solution 2

TDataset.Open is a procedure so you can't get dataset is populated with data or not

procedure TDataSet.Open;
begin
  Active := True;
end;

Active is a property :

Use Active to determine or set whether a dataset is populated with data. When Active is false, the dataset is closed; the dataset cannot read or write data and data-aware controls can not use it to fetch data or post edits. When Active is true, the dataset can be populated with data. It can read data from a database or other source (such as a provider). Depending on the CanModify property, active datasets can post changes. Setting Active to true:

  1. Generates a BeforeOpen event.
  2. Sets the dataset state to dsBrowse.
  3. Establishes a way to fetch data (typically by opening a cursor).
  4. Generates an AfterOpen event.

If an error occurs while opening the dataset, dataset state is set to dsInactive, and any cursor is closed. Setting Active to false:

  1. Triggers a BeforeClose event.
  2. Sets the State property to dsInactive.
  3. Closes the cursor.
  4. Triggers an AfterClose event.

An application must set Active to false before changing other properties that affect the status of a database or the controls that display data in an application.

Note: Calling the Open method sets Active to true; calling the Close method sets Active to false.

Share:
11,315
Admin
Author by

Admin

Updated on June 27, 2022

Comments

  • Admin
    Admin almost 2 years

    Suppose I have a dataset say dsSample in my Delphi application. To read or write data in a dataset, one must open the dataset. I just wanted to know what is the difference between following statements:

    dsSample.Open;
    dsSample.Active := True;
    
    and 
    
    dsSample.Close;
    dsSample.Active := False;
    

    If Open and Active perform same operation, why two different keywords for opening and closing the datasets in Delphi?

  • TLama
    TLama about 10 years
    I think that OP is aware how these things work. This question is more about why there are those two ways of opening and closing datasets.
  • Sayat Ertüfenk
    Sayat Ertüfenk about 10 years
    I explained in first row TDataset.Open is a procedure so you can't get dataset is populated with data or not, but active is a property ...
  • Disillusioned
    Disillusioned about 10 years
    Except that reading Active property tells you nothing about whether the dataset is populated with data. A dataset may be active with 0 records.
  • Disillusioned
    Disillusioned about 10 years
    One additional thing worth mentioning: Active can be used to determine wether a dataset is open. Obviously it shouldn't be used as in the example you provide. But there may be other reasons you wish to confirm if a dataset is active before doing something else.