使用 initfile 打开数据库
如何使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
看起来您的代码似乎是正确的,如果您遇到问题,请确保 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.
如果您的问题出在 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. Typeremote+'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.我发现了我的错误。我应该在由以下命令确定的目录中查找名为“base_dados.mdb”的文件,而不是根据全局变量
remote
检查目录中是否存在名为“ado.mdb”的文件我从 INI 文件中读取的值。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.