开发者

open database with initfile

how to open a database local or remote with IniFile. something like the belo开发者_Python百科w.

   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;


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.


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.


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;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜