views:

92

answers:

3

how to open a database local or remote with IniFile. something like the below.

   vBanco : String; 
   IniFileName : TIniFile; 
begin 
    IniFileName := TIniFile.Create(ExtractFilePath(ParamStr(0))+FileName); 
    Try
        if FileExists (remote+'db\ado.mdb') then
        begin

               vBanco := Trim(IniFileName.ReadString('acesso','BancoRemto','')); 
               Dirlocal := Trim(IniFileName.ReadString('acesso','PastasRemto','')); 
               frmPrincipal.Edit1.text := Dirlocal; 
               Dirtrabalho := (ExtractFilePath(Application.ExeName));

               Conection.ConnectionString := vBanco;
        end
        else
        begin 
           Try 
              vBanco := Trim(IniFileName.ReadString('acesso','banco','')); 
              Dirlocal := Trim(IniFileName.ReadString('acesso','PastasLocais','')); 
              frmPrincipal.Edit1.text := Dirlocal; 
              Dirtrabalho := (ExtractFilePath(Application.ExeName)); 
           Finally 
           end;
        end; 
    Finally
       IniFileName.Free;
    end;
end;
+1  A: 

It seems as if you have your code correct, if you are running into problems make sure the value of the INI Connection string is valid. A Good way to get a valid connect string is to setup the connection at design time then copy and paste it to your config file.

Robert Love
the problem is : with if FileExists ); doesen´t seem to work!!!!!!!! Thank´s
ml
FileExists is a function that has absolutely nothing to do with INI files. If FileExists is the problem, then why is it that the only words in your question are about INI files? And can you please be more specific about what "doesn't seem to work" really means?
Rob Kennedy
A: 

If your problem is with FileExists, here's how you track it down.

Put a breakpoint on the if FileExists line. When it breaks to the debugger, hit CTRL-F7, which will bring up the Evaluate/Modify dialog. Type remote+'db\ado.mdb' into the input box and see what it gives. It'll most likely give you a bad filename.

For example, if remote doesn't end in a backslash, then that will yield an invalid path. You can fix this with the IncludeTrailingPathDelimiter function. Hard to be certain without seeing more of your code, but from my experience that's probably what's going on.

Mason Wheeler
A: 

I found my mistake. Instead of checking the existence of a file named "ado.mdb" in a directory based on the global variable remote, I should have been looking for a file named "base_dados.mdb" in a directory determined by a value I read from an INI file.

procedure TfrmDados.ConectionBeforeConnect(Sender: TObject);
const
  FileName = 'config.ini';
var
  vBanco : String;
  IniFileName : TIniFile;
  LBasedados : String;
begin
  IniFileName := TIniFile.Create(ExtractFilePath(ParamStr(0))+FileName);

  vBanco := Trim(IniFileName.ReadString('acesso','BDtrabalho',''));
  Dirlocal := Trim(IniFileName.ReadString('acesso','Pastrabalho',''));
  LBasedados := Dirlocal+'db\base_dados.mdb';
  frmPrincipal.Edit1.text := Dirlocal;
  Dirtrabalho := (ExtractFilePath(Application.ExeName));
  if FileExists(LBasedados) then
    Conection.ConnectionString := vBanco
  else
  begin
    Application.MessageBox('Bade dados do servidor não encontrada vai trabalhar neste Computador!','Ligação Remota FALHOU');
    vBanco := Trim(IniFileName.ReadString('acesso','BD_local',''));
    Dirlocal := Trim(IniFileName.ReadString('acesso','Pasta_local',''));
    frmPrincipal.Edit1.text := Dirlocal;
    Dirtrabalho := (ExtractFilePath(Application.ExeName));
    Conection.ConnectionString := vBanco;
  end;

  IniFileName.Free;
end;
ml
It seems to me that the root problem was that you didn't understand how your program was *supposed* to work, and you therefore concluded that there was some problem related to INI files or FileExists. Next time, please make sure you understand the goal first.
Rob Kennedy