separate dataset instances using datamodules in delphi

前端 未结 4 1920
小蘑菇
小蘑菇 2021-01-14 12:09

I am using Delphi6 and have a data module with an ADO DataSet which is used by two forms, formA and FormB. Each form has a Datas

4条回答
  •  北海茫月
    2021-01-14 12:59

    The simplest way to achieve what you want is to create an instance of the data module for each form, and pass it to the form so it can be freed when the form is closed:

    var
      Data: TDataModule;
    begin
      Data := T.Create(Self);
      try
        Form := T.Create(Self);
        Form.DataModule := Data;
        Data.Name := '';
      except
        Data.Free;
        raise;
      end;
    
      Form.Show;
    end;
    

    Setting the DataModule's Name to an empty string is done to ensure that the VCL's logic for hooking up data aware controls to their datasource/dataset is done using the newly created instance, instead of the first ever instance.

    In the Form's OnClose handler (or its destructor) make sure to free the data module.

提交回复
热议问题