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.
精彩评论