unit Uconexion;

interface

 uses
  SysUtils, Classes, DB, ADODB, forms, DBCtrls, Buttons, Graphics, IniFiles, JvDBControls;

 procedure InitButtons(navegador:DBCtrls.TDBNavigator; TipoLetra:string);
 Function conectar(conector: TAdoconnection; base: string; directorio: string; pass: string; aplicacion: Tapplication): boolean;
 Function conectarIni(conector: TAdoconnection; base: string; directorio: string): boolean;
  Function conectarAccdb(conector: TAdoconnection; base: string; directorio: string;aplicacion: Tapplication): boolean;
  //procedure InitButtonsJv(navegador:JvDBControls.TJvDBNavigator; TipoLetra:string);
implementation

type
 	TDBNavigator = class(DBCtrls.TDBNavigator);

procedure InitButtons(navegador:DBCtrls.TDBNavigator; TipoLetra:string);
const
  BtnTypeName: array[TNavigateBtn] of String = (
    'Primero', 'Anterior', 'Siguiente', 'Último', 'Añadir',  'Suprimir',
    'Editar', 'Guardar', 'Cancelar', 'Actualizar','aplicar cambios', 'cancelar cambios'
  );
   Rutabnt: array[TNavigateBtn] of String = (
    'primero', 'atras', 'siguiente', 'ultimo', 'nuevo',  'borrar',
    'editar', 'guardar', 'cancelar', 'refrescar','aplicar', 'detener'
  );
var
	I: TNavigateBtn;

begin
	with TDBNavigator(navegador) do
		for I := Low(Buttons) to High(Buttons) do
		begin
      //Buttons[I].Glyph.LoadFromFile('C:\proyectos delphi\Projects\desamparados_plus - sqlserver - emergencia\imagenes\botones\' + Rutabnt[I] + '.bmp');
      Buttons[I].Layout := blGlyphTop;
		 	Buttons[I].Caption := BtnTypeName[I];
      Buttons[I].Font.Name:= TipoLetra;
      Buttons[I].Font.Size:= 10;
      Buttons[I].Font.Style:= [fsbold];
		end;
end;

{procedure InitButtonsJv(navegador:JvDBControls.TJvDBNavigator; TipoLetra:string);
const
  BtnTypeName: array[TNavigateBtn] of String = (
    'Primero', 'Anterior', 'Siguiente', 'Último', 'Añadir',  'Suprimir',
    'Editar', 'Guardar', 'Cancelar', 'Actualizar','aplicar cambios', 'cancelar cambios'
  );

var
	I: TNavigateBtn;

begin
	with TDBNavigator(navegador) do
		for I := Low(Buttons) to High(Buttons) do
		begin
     	Buttons[I].Caption := BtnTypeName[I];
      Buttons[I].Font.Name:= TipoLetra;
      Buttons[I].Font.Size:= 10;
      Buttons[I].Font.Style:= [fsbold];
      Buttons[I].Layout := blGlyphTop;
		end;
end;  }


 Function conectar(conector: TAdoconnection; base: string; directorio: string; pass: string; aplicacion: Tapplication): boolean;
  var
   ruta, cadena: string;
  begin
  try
    conector.Connected:= false;
    ruta:= ExtractFilePath(application.ExeName) + '\'+ directorio + '\'+base + '.mdb';
    cadena:= 'Provider=Microsoft.Jet.OLEDB.4.0;Password="";User ID=Admin;Data Source=' + ruta + ';Mode=ReadWrite;Jet OLEDB:Database Password='+pass;
    Conector.ConnectionString := cadena;
    conector.Connected:= true;
    result:= true;
   except
     result:= false;
   end
  end;

  Function conectarIni(conector: TAdoconnection; base: string; directorio: string): boolean;
  var
   cadena: string;
   FileIni: TIniFile;
   direccion: string;
  begin
   //dire:= extractfilePath(directorio);
   if FileExists(directorio + '\config.ini') then
    begin
     FileIni := TIniFile.Create(directorio + '\config.ini');
     direccion:=FileIni.ReadString('Conexion','Cadena','');
     cadena:= 'Provider=Microsoft.ACE.OLEDB.12.0;User ID=Admin;Data Source=' + Direccion + ';Mode=ReadWrite;';
     Conector.connectionString:= Cadena;
     Conector.open;
     FileIni.free;
     Result:= true
    end
    else
     Result:= False;
  end;

   //Esta función permite conectar una base de datos de access accdb, formato 2007, 2010, 2013
  Function conectarAccdb(conector: TAdoconnection; base: string; directorio: string; aplicacion: Tapplication): boolean;
  var
   ruta, cadena: string;
  begin
  try
    conector.Connected:= false;
    ruta:= ExtractFilePath(application.ExeName) + '\'+ directorio + '\'+ base + '.accdb';
    cadena:= 'Provider=Microsoft.ACE.OLEDB.12.0;Password="";Data Source=' + ruta + ';Persist Security Info=True';
    Conector.ConnectionString := cadena;
    conector.Connected:= true;
    result:= true;
   except
     result:= false;
   end
  end;
end.
