2012-10-16 39 views
1

空间?我需要访问其中一些客户的路径中有空格的文件夹。无法打开DBF在使用以下工作路径

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; 

执行一次 Connection.Open(); 它正确打开它,

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

我得到{System.Data.Odbc.OdbcException:ERROR [42S02] [Microsoft] [ODBC Visual FoxPro Driver] File'file2.dbf'不存在。


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

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

总之,我在连接字符串和命令字符串的路径前面使用了@ @

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

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

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

OdbcDataReader Reader = Command.ExecuteReader(); 
+0

这是公平地说,它没有连接到数据库,但它会很高兴地知道实际的错误信息是什么 –

回答

3

你已经尝试把路径进入报价?

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

很好用的转义字符代替'code' Connection.ConnectionString = @“D River = {Microsoft Visual FoxPro Driver}; Exclusive = No; SourceType = DBF; SourceDB = \“+ strFilename +”\“”;'code' – Roman

+0

Did it work?... –

+0

No it did not work。不知道它是否可以成为FoxPro驱动程序。我知道它与MS-DOS命名约定有关。我仍然得到System.Data.Odbc.OdbcException:错误[42S02] [Microsoft] [ODBC Visual FoxPro驱动程序]文件'file2.dbf'不存在。 – Roman