unit uConexionBD;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, 
  Dialogs, StdCtrls, ExtCtrls, cxShellBrowserDialog, cxControls,
  cxContainer, cxEdit, cxTextEdit, cxMaskEdit, cxButtonEdit, IBServices,
  DB, IBDatabase, uFrameConfiguracion, DBXpress, SqlExpr, uDAClasses;

type
  TfrConexionBD = class(TFrameConfiguracion)
    GroupBox1: TGroupBox;
    edtServer: TEdit;
    Label1: TLabel;
    Label3: TLabel;
    bProbar: TButton;
    Label4: TLabel;
    edtUser: TEdit;
    Label5: TLabel;
    edtPassword: TEdit;
    Bevel1: TBevel;
    edtBD: TEdit;
    SQLConnection: TSQLConnection;
    procedure bProbarClick(Sender: TObject);
  protected
    procedure Finalize; override;
  public
    procedure Init; override;
  end;

implementation

uses uDataModuleServer;

{$R *.dfm}

procedure TfrConexionBD.bProbarClick(Sender: TObject);
var
  Aux : string;
begin
  with SQLConnection do
  begin
    Connected := False;
    with Params do
    begin
      Clear;
      Add('DriverName=MSSQL');
      Add('HostName=' + edtServer.Text);
      Add('DataBase=' + edtBD.Text);
      Add('User_Name=' + edtUser.Text);
      Add('Password=' + edtPassword.Text);
      Add('BlobSize=-1');
      Add('ErrorResourceFile=');
      Add('LocaleCode=0000');
      Add('MSSQL TransIsolation=ReadCommited');
      Add('OS Authentication=False');
    end;

    try
      Connected := True;
      Application.MessageBox('Conexión válida con la base de datos.', 'Información', MB_OK);
      Connected := False;
    except
      on E : Exception do
      begin
        Aux := 'No se ha podido establecer la conexión con la base de datos.'
          + #13 + #10 + #13 + #10 + PChar(E.Message);
        Application.MessageBox(PAnsiChar(Aux), 'Error', MB_OK);
      end;
    end;
  end;
end;

procedure TfrConexionBD.Init;
begin
  edtServer.Text := dmServer.FDBServer;
  edtBD.Text := dmServer.FDBName;
  edtUser.Text := dmServer.FDBUser;
  edtPassword.Text := Decrypt(dmServer.FDBPass);
end;

procedure TfrConexionBD.Finalize;
begin
  dmServer.FDBServer := edtServer.Text;
  dmServer.FDBName := edtBD.Text;
  dmServer.FDBUser := edtUser.Text;
  dmServer.FDBPass := Encrypt(edtPassword.Text);
end;

end.
