unit uConexionBD_MSSQL;

interface

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

type
  TfrConexionBD_MSSQL = class(TFrameConfiguracion)
    GroupBox1: TGroupBox;
    edtServer: TEdit;
    edtPort: TEdit;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    bProbar: TButton;
    Label4: TLabel;
    edtUser: TEdit;
    Label5: TLabel;
    edtPassword: TEdit;
    Bevel1: TBevel;
    OpenDialog: TOpenDialog;
    edtDatabaseName: TEdit;
    ADOConnection1: TADOConnection;
    procedure bProbarClick(Sender: TObject);
    procedure edtPortExit(Sender: TObject);
  protected
    procedure Finalize; override;
  public
    procedure Init; override;
  end;

implementation

uses uDataModuleServer;

{$R *.dfm}

procedure TfrConexionBD_MSSQL.bProbarClick(Sender: TObject);
var
  Aux : string;
  AConnStr : string;
begin
  AConnStr := 'Provider=sqloledb;' +
            'Data Source=' + edtServer.Text + ';' +
            'Initial Catalog=' + edtDatabaseName.Text + ';' +
            'User Id=' + edtUser.Text + ';' +
            'Password=' + edtPassword.Text;

  with ADOConnection1 do
  begin
    Close;
    ConnectionString := AConnStr;
    LoginPrompt := False;

    try
      Open;
      Application.MessageBox('Conexión válida con la base de datos de Farmatic.', 'FactuGES', MB_OK);
      Close;
    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_MSSQL.Init;
begin
  edtServer.Text := dmServer.FMSSQLServer;
  edtPort.Text := dmServer.FMSSQLPort;
  edtDatabaseName.Text := dmServer.FMSSQLPath;
  edtUser.Text := dmServer.FMSSQLUser;
  edtPassword.Text := Decrypt(dmServer.FMSSQLPass);
end;

procedure TfrConexionBD_MSSQL.Finalize;
begin
  dmServer.FMSSQLServer := edtServer.Text;
  dmServer.FMSSQLPort := edtPort.Text;
  dmServer.FMSSQLPath := edtDatabaseName.Text;
  dmServer.FMSSQLUser := edtUser.Text;
  dmServer.FMSSQLPass := Encrypt(edtPassword.Text);
end;

procedure TfrConexionBD_MSSQL.edtPortExit(Sender: TObject);
begin
  inherited;
  if Length(edtPort.Text) = 0 then
    edtPort.Text := MSSQL_PORT;
end;

end.
