Ver Mensaje Individual
  #4  
Antiguo 30-03-2023
Avatar de TrUnkS
TrUnkS TrUnkS is offline
Miembro
 
Registrado: dic 2005
Posts: 93
Reputación: 19
TrUnkS Va por buen camino
Lo ideal es tener varios DataModules en la misma aplicación, de modo que pongas el componente de conexión solo y abandonado en el UserSession (FDConnection). Los otros componentes FDQuery los pongas en cada DataModulo, y que cada formulario IWForm este asociado a un DataModulo. Entonces quedaría así:

El UserSession:

Código Delphi [-]
unit UserSessionUnit;

{
  This is a DataModule where you can add components or declare fields that are specific to 
  ONE user. Instead of creating global variables, it is better to use this datamodule. You can then
  access the it using UserSession.
}
interface

uses
  IWUserSessionBase, SysUtils, Classes, FireDAC.Stan.Intf, FireDAC.Stan.Option,
  FireDAC.Stan.Error, FireDAC.UI.Intf, FireDAC.Phys.Intf, FireDAC.Stan.Def,
  FireDAC.Stan.Pool, FireDAC.Stan.Async, FireDAC.Phys, FireDAC.Phys.MSSQL,
  FireDAC.Phys.MSSQLDef, FireDAC.VCLUI.Wait, Data.DB, FireDAC.Comp.Client;

type
  TIWUserSession = class(TIWUserSessionBase)
    FDConnectionDB: TFDConnection;
  private
    { Private declarations }
  public
    { Public declarations }
  end;

implementation

{$R *.dfm}

end.

El Módulo de datos:

Código Delphi [-]
unit DMFormLOGIN;

interface

uses
  System.SysUtils, System.Classes, FireDAC.Stan.Intf, FireDAC.Stan.Option,
  FireDAC.Stan.Param, FireDAC.Stan.Error, FireDAC.DatS, FireDAC.Phys.Intf,
  FireDAC.DApt.Intf, FireDAC.Stan.Async, FireDAC.DApt, Data.DB,
  FireDAC.Comp.DataSet, FireDAC.Comp.Client;

type
  TIWDMFormLOGIN = class(TDataModule)
    FDQueryTEAM: TFDQuery;
    procedure DataModuleCreate(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  IWDMFormLOGIN: TIWDMFormLOGIN;

implementation

{%CLASSGROUP 'Vcl.Controls.TControl'}

uses ServerController;

{$R *.dfm}

procedure TIWDMFormLOGIN.DataModuleCreate(Sender: TObject);
begin
 FDQueryTEAM.Connection := UserSession.FDConnectionDB;
end;

end.

Y el Formulario:

Código Delphi [-]
unit FormLOGIN;

interface

uses
  Classes, SysUtils, IWAppForm, IWApplication, IWColor, IWTypes, DMFormLOGIN;

type
  TIWFormLOGIN = class(TIWAppForm)
    procedure IWAppFormCreate(Sender: TObject);
    procedure IWAppFormDestroy(Sender: TObject);
    procedure IWAppFormShow(Sender: TObject);
  private
    IWDMFormLOGIN: TIWDMFormLOGIN;
  public
  end;

implementation

{$R *.dfm}


procedure TIWFormLOGIN.IWAppFormCreate(Sender: TObject);
begin
 if IWDMFormLOGIN = nil then // se puede omitir esta linea
  IWDMFormLOGIN := TIWDMFormLOGIN.Create(Self);
end;

procedure TIWFormLOGIN.IWAppFormDestroy(Sender: TObject);
begin
 if Assigned(IWDMFormLOGIN) then // se puede omitir esta línea
  FreeAndNil(IWDMFormLOGIN);
end;

procedure TIWFormLOGIN.IWAppFormShow(Sender: TObject);
begin
 IWDMFormLOGIN.FDQueryTEAM.Open;
end;

initialization
  TIWFormLOGIN.SetAsMainForm;

end.

Última edición por Casimiro Notevi fecha: 30-03-2023 a las 23:06:59. Razón: Poner etiquetas [delphi] [/delphi] al código.
Responder Con Cita