使用 initfile 打开数据库

发布于 2024-09-04 10:26:14 字数 1055 浏览 9 评论 0原文

如何使用 IniFile 打开本地或远程数据库。 像下面这样的东西。

   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;

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;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

天赋异禀 2024-09-11 10:26:14

看起来您的代码似乎是正确的,如果您遇到问题,请确保 INI 连接字符串的值有效。获取有效连接字符串的一个好方法是在设计时设置连接,然后将其复制并粘贴到配置文件中。

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.

对岸观火 2024-09-11 10:26:14

如果您的问题出在 FileExists 上,请按以下方法查找问题。

if FileExists 行放置一个断点。当它中断到调试器时,按 CTRL-F7,这将打开“评估/修改”对话框。在输入框中输入 remote+'db\ado.mdb' 并查看它给出的内容。它很可能会给你一个错误的文件名。

例如,如果 remote 不以反斜杠结尾,那么将产生无效路径。您可以使用 IncludeTrailingPathDelimiter 函数修复此问题。如果没有看到更多代码,很难确定,但根据我的经验,这可能就是正在发生的事情。

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.

信仰 2024-09-11 10:26:14

我发现了我的错误。我应该在由以下命令确定的目录中查找名为“base_dados.mdb”的文件,而不是根据全局变量remote检查目录中是否存在名为“ado.mdb”的文件我从 INI 文件中读取的值。

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;

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;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文