开发者

IErrorInfo.GetDescription failed with E_FAIL(0x80004005).System.Data while data adapter Fill()

开发者 https://www.devze.com 2023-03-15 10:13 出处:网络
I\'m trying to get data from CSV file , by usingfill() methodi got a exception idk why it arise,please review codeand suggestoptimistic answer.note that if parameter \"s\" doesn\'t have any space mean

I'm trying to get data from CSV file , by using fill() method i got a exception idk why it arise,please review code and suggest optimistic answer.note that if parameter "s" doesn't have any space means it works fine. if it have space means how to overcome that,don't suggest temporary rename and all.

/// <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();
        r开发者_如何学运维eturn ds.Tables[0];
    }
    catch (Exception ex)
    {
        Trace.WriteLine(ex.Message + ex.Source);
        return null;
    }
}


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

near the tilde key..


I have faced the same issue, but was able to overcome it by enclosing the suspect word in square brackets.

In my case, it was a field name which I used in "Where" condition i.e.;

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


In my case replacing the whole field names in query by * solved the issue.

0

精彩评论

暂无评论...
验证码 换一张
取 消