当文件路径/名称有空格时,使用 System.Data.Odbc.OdbcConnection 查询 DBF 文件

发布于 2024-09-08 01:54:16 字数 521 浏览 1 评论 0原文

我正在尝试使用 System.Data.Odbc.OdbcConnection 查询 DBF 文件。当文件中没有空格时它可以正常工作,但如果文件路径或名称中有一个空格。

我使用以下代码:

oConn = new System.Data.Odbc.OdbcConnection(); oConn.ConnectionString = "Driver={Microsoft dBase Driver (*.dbf)};SourceType=DBF;SourceDB=NA;Exclusive=No; Collat​​e=Machine;NULL=NO;DELETED=NO;BACKGROUNDFETCH=NO;";

oCmd.CommandText = "从 C:\test 2\12345678.dbf 选择 *";

命令文本不是硬编码的。为了简单起见,我只是这样包含它。该应用程序设置为允许用户选择 DBF 文件并显示它。我无法控制用户存储 DBF 文件的位置,并且不想让他们记住不要在文件名/路径中添加空格。

如何转义文件名/路径中的空格?

I'm trying to query a DBF file using System.Data.Odbc.OdbcConnection. It works correctly when the file doesn't have a space in it, but I get the following error "Error opening DBF File: ERROR [42000][Microsoft][ODBC dBase Driver]Syntax error in FROM clause" if the file path or name has a space in it.

I'm using the following code:

oConn = new System.Data.Odbc.OdbcConnection();
oConn.ConnectionString = "Driver={Microsoft dBase Driver (*.dbf)};SourceType=DBF;SourceDB=NA;Exclusive=No; Collate=Machine;NULL=NO;DELETED=NO;BACKGROUNDFETCH=NO;";

oCmd.CommandText = "SELECT * FROM C:\test 2\12345678.dbf";

The command text isn't hard-coded. I just included it that way for simplicity. The application is setup to allow a user to pick a DBF file and have it display it. I don't have control over where the users store the DBF files and would rather not have to have them remember not to put spaces in the file name/path.

How do I escape the space in the file name/path?

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

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

发布评论

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

评论(2

花伊自在美 2024-09-15 01:54:16

可能是与“MS-DOS 8.3 文件名格式”相关的问题。
您可以查看以下链接:

Probable it's a problem related with the "MS-DOS 8.3 file name format" .
You can review the next links:

聚集的泪 2024-09-15 01:54:16

我也遇到了这个问题。这是我在谷歌排名第一的热门产品,所以我并没有抱太大希望。但是,我们能够通过将当前目录更改为有问题的目录,然后排除 CommandText 中的路径来使我的代码正常工作:

//Save the current directory
string currentDir = System.IO.Directory.GetCurrentDirectory();

//Select the path that we need to use
System.IO.Directory.SetCurrentDirectory("C:\\test 2\\");

//Now the path isn't required:
oCmd.CommandText = @"SELECT * FROM 12345678.dbf";

//Restore the old directory
System.IO.Directory.SetCurrentDirectory(currentDir);

您仍然受到文件名的限制(没有空格和 <= 8 个字符)我想),但这是我可以合作的。

I was running into this issue as well. This was my #1 hit in Google, so I wasn't very hopeful. However, we were able to get my code to work by changing the current directory to the problematic directory and then excluding the path in the CommandText:

//Save the current directory
string currentDir = System.IO.Directory.GetCurrentDirectory();

//Select the path that we need to use
System.IO.Directory.SetCurrentDirectory("C:\\test 2\\");

//Now the path isn't required:
oCmd.CommandText = @"SELECT * FROM 12345678.dbf";

//Restore the old directory
System.IO.Directory.SetCurrentDirectory(currentDir);

You're still limited in what the file's name can be (no spaces and <= 8 char I think), but that's something I can work with.

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