0

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;
4
  • 1
    INI files do not open databases. What are you actually trying to do? Which part of that code is relevant to your question? Could you please remove any code that isn't relevant? Jun 7, 2010 at 23:32
  • @ml: According to your profile, you've asked seven questions so far, gotten answers to five of them, and not accepted any of the answers. It's considered polite here on StackOverflow to mark a good answer as accepted (click the checkbox icon below the voting widget) so the rest of us know you've found a satisfactory answer. Jun 8, 2010 at 0:09
  • Rob and Mason: Pardon my unsolicited comment but the edits that you two add to this forum immensely improve the quality of the information that the rest of us can find here. I don't know how you find the time to keep up but it's one of the main reasons that I make the time to follow "Questions tagged Delphi" on StackOverflow in my RSS aggregator every day. I know it's not easy being the cop. Thanks for the great work.
    – jrodenhi
    Jun 9, 2010 at 16:31
  • (offtopic) I think you should try to open database with JPEG file! :D
    – Alex
    Jun 10, 2010 at 14:45

3 Answers 3

1

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.

1
  • 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? Jun 7, 2010 at 23:34
0

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.

0

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;
1
  • 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. Jun 10, 2010 at 14:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.