【问题标题】:Cannot open DBF with spaces in the path无法打开路径中有空格的 DBF
【发布时间】:2012-10-16 15:39:30
【问题描述】:

当目录名中没有空格时使用以下方法

对于字符串file1,它将使用字符串file2执行reader,它会创建一个异常吗?对于我们的一些客户,我需要访问路径中有空格的文件夹。

file1 = "C:\\test1\\file.dbf";
file2 = "C:\\test 2\\file.dbf";

OdbcConnection Connection = new OdbcConnection();
Connection.ConnectionString = @"Driver={Microsoft Visual FoxPro Driver};Exclusive=No;SourceType=DBF;SourceDB=" + strFilename + ";";
Connection.Open();

OdbcCommand Command = Connection.CreateCommand();
Command.CommandText = @"SELECT * FROM " + file1; //Command.CommandText = @"SELECT * FROM " + file2;

一旦执行 连接.打开(); 它正确打开它,

一旦我们执行 OdbcDataReader Reader = Command.ExecuteReader();

我得到 {System.Data.Odbc.OdbcException: ERROR [42S02] [Microsoft][ODBC Visual FoxPro Driver]File 'file2.dbf' does not exist.


回答,因为我没有足够的代表

感谢大家对我的问题的解决方案的贡献。该解决方案要求我放置完整路径(其中有一个空格)并使用 @ char 使其成为文字字符串。出于某种原因,通过在引号中加上转义字符(“\””)使其文字化并不能解决问题。

综上所述,我在连接字符串和命令字符串中的路径前面都使用了@。

strFilename = S.ImportFolder + "\\" +"file"+ ".dbf";

OdbcConnection Connection = new OdbcConnection();
Connection.ConnectionString = @"Driver={Microsoft Visual FoxPro Driver};Exclusive=No;SourceType=DBF;SourceDB="+@strFilename+";";
Connection.Open();

OdbcCommand Command = Connection.CreateCommand();
Command.CommandText = @"SELECT * FROM "+ @strFilename;

OdbcDataReader Reader = Command.ExecuteReader();

【问题讨论】:

  • 可以公平地说,它没有连接到数据库,虽然很高兴知道实际的错误消息是什么。

标签: c#


【解决方案1】:

您是否已经尝试将路径放在引号中?

Connection.ConnectionString = @"Driver={Microsoft Visual FoxPro Driver};Exclusive=No;SourceType=DBF;SourceDB=""" + strFilename + """;";

【讨论】:

  • 很好地使用了转义字符 code Connection.ConnectionString = @"Driver={Microsoft Visual FoxPro Driver};Exclusive=No;SourceType=DBF;SourceDB= \" + strFilename + "\"" ;code
  • 不,它没有用。想知道它是否可以是 FoxPro 驱动程序。我知道这与 MS-DOS 命名约定有关。我仍然得到 System.Data.Odbc.OdbcException: ERROR [42S02] [Microsoft][ODBC Visual FoxPro Driver]File 'file2.dbf' does not exist。
  • @Roman 如果您在开头使用@,则引号必须用另一个引号("")转义。将两者混合会导致意想不到的结果(例如"...SourceDB=\C:\test 2\file.dbf")。
猜你喜欢
  • 2011-12-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-04-30
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多