How to Connect to Database by Reading INI File

Asked

Viewed 1,076 times

-2

  • The method that is on the link is what you created to try to connect to the bank? Can you already read the file . ini? Explain a little better

  • no, my friend who made it for me lol

  • i want that method read the ini file, and make the connection to bank

2 answers

1


This is the way I know how to 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 file . INI there is the Key and the Subchave as in the example below:

[CHAVE]
subchave=valor

Your file would look +/- like this (The data I used is fictitious):

[CONEXAO]
Database=Pangya
User_Name=sa
Password=1
Server=127.0.0.1
[OUTROSDADOS]
subOutrosDados=tal
subOutrosDadosDois=
  • Obgd for the help !

  • one last help if possible, give to show how the ini file would be ? grateful !

  • Of course, see the Editing of my reply and if it is useful and helped you do not forget to mark as accepted please! D

0

no seu codigo faltou a namespace "System.SysUtils" se não vai dar erro.

dai tried to run the . exe ran, but nothing happened simply closes alone but I guess it did not connect. I do not know if it makes a difference

[OUTROSDADOS]

subOther Data=such subOther=

  • It makes no difference being empty, if the program opens and closes, it must be something you did wrong in your project. Debuggue to see what can be!

  • I changed some things and it worked

  • It took me a long time to answer because as you answered your own question and didn’t come notification to me... So when so, instead of answering your question, use the comments area here then the person is notified.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.