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();
return 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..