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, ADODB;

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;
    ADOConnection: TADOConnection;
    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
  //Provider=SQLOLEDB.1;Persist Security Info=False;User ID=sa;Initial Catalog=gft_varela;Data Source=david

  Aux := 'Provider=SQLOLEDB.1;Persist Security Info=False';
  Aux := Aux + ';Data Source=' + edtServer.Text;
  Aux := Aux + ';User ID=' + edtUser.Text;
  Aux := Aux + ';Password=' + edtPassword.Text;
  Aux := Aux + ';Initial Catalog=' + edtBD.Text;

  with ADOConnection do
  begin
    LoginPrompt := False;
    Mode := cmRead;
    Provider := 'SQLOLEDB.1';
    ConnectionString := Aux;

    ShowHourglassCursor;
    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
        HideHourglassCursor;
        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.
