2012-10-16 71 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