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

type
  TfrConexionBD_MSSQL = class(TFrameConfiguracion)
    GroupBox1: TGroupBox;
    edtServer: TEdit;
    Label1: TLabel;
    Label3: TLabel;
    bProbar: TButton;
    Label4: TLabel;
    edtUser: TEdit;
    Label5: TLabel;
    edtPassword: TEdit;
    Bevel1: TBevel;
    edtDatabaseName: TEdit;
    ADOConnection1: TADOConnection;
    cbAutentSQL: TCheckBox;
    ActionList1: TActionList;
    actAutentSQL: TAction;
    procedure bProbarClick(Sender: TObject);
    procedure actAutentSQLExecute(Sender: TObject);
  protected
    procedure Finalize; override;
  public
    procedure Init; override;
  end;

implementation

uses uDataModuleServer;

{$R *.dfm}

procedure TfrConexionBD_MSSQL.actAutentSQLExecute(Sender: TObject);
begin
  inherited;
  edtUser.Enabled := not edtUser.Enabled;
  Label4.Enabled := edtUser.Enabled;

  edtPassword.Enabled := not edtPassword.Enabled;
  Label5.Enabled := edtPassword.Enabled;
end;

procedure TfrConexionBD_MSSQL.bProbarClick(Sender: TObject);
var
  Aux : string;
  AConnStr : string;
begin
  //Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=bd;Data Source=servidor
  //Provider=SQLOLEDB.1;Password=assasasasa;Persist Security Info=True;User ID=11111111;Initial Catalog=aaaaa;Data Source=aaaa
  AConnStr := 'Provider=SQLOLEDB.1;' +
            'Data Source=' + edtServer.Text + ';' +
            'Initial Catalog=' + edtDatabaseName.Text + ';';

  if not cbAutentSQL.Checked then
    AConnStr := AConnStr + 'Integrated Security=SSPI;Persist Security Info=False;'
  else
    AConnStr := AConnStr + '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.', 'Etiquetas', 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;
  cbAutentSQL.Checked := (dmServer.FMSSQLIntSecurity = 1);
  edtDatabaseName.Text := dmServer.FMSSQLPath;
  edtUser.Text := dmServer.FMSSQLUser;
  edtPassword.Text := Decrypt(dmServer.FMSSQLPass);
end;

procedure TfrConexionBD_MSSQL.Finalize;
begin
  dmServer.FMSSQLServer := edtServer.Text;
  if cbAutentSQL.Checked then
    dmServer.FMSSQLIntSecurity := 1
  else
    dmServer.FMSSQLIntSecurity := 0;
  dmServer.FMSSQLPath := edtDatabaseName.Text;
  dmServer.FMSSQLUser := edtUser.Text;
  dmServer.FMSSQLPass := Encrypt(edtPassword.Text);
end;

end.
