【问题标题】:IErrorInfo.GetDescription failed with E_FAIL(0x80004005).System.Data while data adapter Fill()IErrorInfo.GetDescription 在数据适配器 Fill() 时出现 E_FAIL(0x80004005).System.Data 失败
【发布时间】:2011-09-21 07:19:04
【问题描述】:

我正在尝试从 CSV 文件中获取数据,通过使用 fill() 方法,我得到了一个异常 idk 为什么会出现,请查看代码并提出乐观的答案。请注意,如果参数“s”没有任何空间意味着它工作正常。如果它有空间意味着如何克服它,不建议临时重命名。

/// <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;
    }
}

【问题讨论】:

    标签: c# .net excel-2007


    【解决方案1】:
    var da = new OleDbDataAdapter("SELECT * FROM `" + file + "`", sConnection);
    

    在波浪号附近..

    【讨论】:

    • 已修复,需要在文件名前后使用`字符,有空格
    【解决方案2】:

    我也遇到过同样的问题,但是通过将可疑词括在方括号中来克服它。

    在我的情况下,它是我在“Where”条件下使用的字段名称,即;

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

    【讨论】:

      【解决方案3】:

      在我的情况下,将查询中的整个字段名称替换为 * 解决了这个问题。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-03-27
        • 1970-01-01
        • 2023-03-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-09-04
        • 1970-01-01
        相关资源
        最近更新 更多