2011-06-23 59 views
2

我試圖從CSV文件獲取數據,通過填寫()方法,我有一個例外IDK的爲什麼它出現時,請查看代碼並建議樂觀的答案。如果參數「s」沒有任何空格意味着它可以正常工作。如果有空間意味着如何克服這一點,不建議臨時重命名和所有。IErrorInfo.GetDescription失敗,E_FAIL(0x80004005的).System.Data而數據適配器填充()

/// <summary> 
/// Import Function For CSV Delimeted File 
/// </summary> 
/// <param name="s">File Name</param> 
private DataTable Import4csv(string s) 
{ 
    string file = Path.GetFileName(s); 
    string dir = Path.GetDirectoryName(s); 
    string sConnection = "Provider=Microsoft.Jet.OLEDB.4.0;" 
          + "Data Source=\"" + dir + "\\\";" 
          + "Extended Properties=\"text;HDR=YES;FMT=Delimited\""; 
    try 
    { 
     var objConn = new OleDbConnection(sConnection); 
     objConn.Open(); 
     var ds = new DataSet(); 
     var da = new OleDbDataAdapter("SELECT * FROM " + file, sConnection); 
     da.Fill(ds);  // getting exception on this line. 
     objConn.Close(); 
     return ds.Tables[0]; 
    } 
    catch (Exception ex) 
    { 
     Trace.WriteLine(ex.Message + ex.Source); 
     return null; 
    } 
} 

回答

6
var da = new OleDbDataAdapter("SELECT * FROM `" + file + "`", sConnection); 

附近的波浪鍵..

+0

它不工作。 –

+0

固定它,你需要之前有空格的文件名後使用'字符 – shobhonk

3

我也面臨着同樣的問題,但能夠通過封閉在方括號中的可疑單詞來克服它。

在我而言,這是我在「去哪兒」條件即使用的字段名稱;

select * from tblDetail where [Section] = 'Operations' 
0

在我的情況下,通過更換*查詢整場名稱解決的問題。

相關問題