2011-06-23 47 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

在我的情况下,通过更换*查询整场名称解决的问题。

相关问题