unit uDBConnection;

interface

uses
  System.SysUtils, System.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.MySQLDef, FireDAC.VCLUI.Wait, FireDAC.Comp.UI, FireDAC.Phys.MySQL,
  Data.DB, FireDAC.Comp.Client, vcl.Dialogs;

type
  TDBConnection = class(TDataModule)
    FDConnection: TFDConnection;
    FDPhysMySQLDriverLink: TFDPhysMySQLDriverLink;
    FDGUIxWaitCursor: TFDGUIxWaitCursor;
    procedure DataModuleCreate(Sender: TObject);
    procedure DataModuleDestroy(Sender: TObject);
  private
    { Private declarations }
  public

   procedure Connect;
   function Connected:Boolean;

    { Public declarations }
  end;

var
  DBConnection: TDBConnection;

implementation

{%CLASSGROUP 'Vcl.Controls.TControl'}

{$R *.dfm}

{ TConnection }

procedure TDBConnection.Connect;
var oParams:Tstrings;
begin
      oParams := TStringList.Create;
    //  oParams.Add('DriverID='+DriverID);
      oParams.Add('DataBase=treeview_db');
      oParams.Add('Password=masterkey');
      oParams.Add('User_Name=root');
      oParams.Add('Port=3306');
      oParams.Add('Server=localhost');
      oParams.Add('CharacterSet=utf8');

       FDConnection.Params.Assign(oParams);
       FDConnection.DriverName:='MySQL';

       try
        FDConnection.Connected:=true;
       except
        showmessage('Не получилось соединиться с базой данных');
       end;



       oParams.Free;
end;


  function TDBConnection.Connected: Boolean;
    begin

    if FDConnection.Connected then Result:=true
    else Result:=false;

    end;

procedure TDBConnection.DataModuleCreate(Sender: TObject);
begin
Self.Connect;
end;

procedure TDBConnection.DataModuleDestroy(Sender: TObject);
begin
Self.FDConnection.Connected:=false;
end;

end.
