How to Connect to the database reading INI file

-2

I need to make the console read the ini file to try to connect to the database in delphi 10.2

I do not know how to deal with delphi

this link is the method where the database is link

    
asked by anonymous 22.05.2018 / 18:24

2 answers

1

This is how I can connect using a FDConnection

Change the data according to what you need

unit FiredacPooling;

interface

uses
  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.MySQL, FireDAC.Phys.MySQLDef, FireDAC.Stan.Param,
  FireDAC.DatS, FireDAC.DApt.Intf, FireDAC.DApt, Data.DB, FireDAC.Comp.DataSet,
  FireDAC.Comp.Client, Classes, IniFiles;

type
  TFireDacPooling = class
    private
      FConnection : TFDConnection;
      function CarregaConfiguracoes(cChave, cSubChave: String; cValorPadrao: string = ''): Variant;
      procedure AtualizarConfiguracoes(cChave, cSubChave: String; cValor: string = '');  
    public
      constructor Create;
      destructor Destroy; override;
  end;

var
  DBPool : TFireDacPooling;

implementation

{ TFireDacPooling }

constructor TFireDacPooling.Create;
{var
  Params: TStringList;}
begin
  {Params := TStringList.Create;
  try
    //Params.Add('User_Name=sa');
    //Params.Add('Password=1');
    //Params.Add('Server=127.0.0.1');
    Params.Add('Server=DESKTOP-3RKGJ2M');
    Params.Add('OSAuthent=Yes');
    Params.Add('Database=Pangya');
    Params.Add('Pooled=True');
    Params.Add('POOL_MaximumItems=1000');

    FConnection := TFDManager.Create(nil);
    FConnection.AddConnectionDef('MSSQLPool', 'MSSQL', Params);

  finally
    Params.Free;
  end;}

  with(FConnection)do
  begin
    if(Connected)then
      Connected := False;
    Params.Values['DriverID']          := 'MSSQL';
    Params.Values['Database']          := CarregaConfiguracoes('ChavePrincipal', 'ChaveSecundaria'); {Pangya}
    Params.Values['User_Name']         := CarregaConfiguracoes('ChavePrincipal', 'ChaveSecundaria'); {sa}
    Params.Values['Password']          := CarregaConfiguracoes('ChavePrincipal', 'ChaveSecundaria'); {1}
    Params.Values['Pooled']            := True;
    Params.Values['POOL_MaximumItems'] := 1000;
    Params.Values['OSAuthent']         := True;
    Params.Values['Server']            := CarregaConfiguracoes('ChavePrincipal', 'ChaveSecundaria'); {DESKTOP-3RKGJ2M}

    Connected := True;
  end;  
end;

destructor TFireDacPooling.Destroy;
begin
  FConnection.Free;
  inherited;
end;

function TFireDacPooling.CarregaConfiguracoes(cChave, cSubChave: String; cValorPadrao: string = ''): Variant;
var
  oConfig: TIniFile;
begin
  Result  := '';               {Carrega o arquivo .ini, mas para isso o mesmo precisa estar no mesmo diretório do EXE}
  oConfig := TIniFile.Create(ExtractFileDir(ExtractFilePath(ParamStr(0))) + '\SeuArquivo.ini');

  try
    Result := oConfig.ReadString(cChave, cSubChave, cValorPadrao);
  finally
    oConfig.Free;
  end;
end;

procedure TFireDacPooling.AtualizarConfiguracoes(cChave, cSubChave: String; cValor: string = '');
var
  oConfig: TIniFile;
begin                          {Escreve no arquivo .ini, mas para isso o mesmo precisa estar no mesmo diretório do EXE}
  oConfig := TIniFile.Create(ExtractFileDir(ExtractFilePath(ParamStr(0))) + '\SeuArquivo.ini');

  try
    oConfig.WriteString(cChave, cSubChave, cValor);
  finally
    oConfig.Free;
  end;
end;

initialization
  DBPool := TFireDacPooling.Create;
finalization
  DBPool.Free;

end.

Edited

In the .INI file there is the Key and SubChave as in the example below:

[CHAVE]
subchave=valor

Your file would be +/- so (The data I used there are dummy):

[CONEXAO]
Database=Pangya
User_Name=sa
Password=1
Server=127.0.0.1
[OUTROSDADOS]
subOutrosDados=tal
subOutrosDadosDois=
    
23.05.2018 / 20:39
0
no seu codigo faltou a namespace "System.SysUtils" se não vai dar erro.

I tried to run the .exe file, but nothing happened, it simply closes itself but I think it did not connect. I do not know if it makes a difference.

[OUTROSDADOS]

SubOutrosDados = tal SubDoesDouble =

    
05.06.2018 / 15:52