This method does not throw an exception, neither does it return the rows from the csv file it reads into the DataTable. I am at a loss for what I am doing incorrectly.
Any help is much appreciated.
public string ImportCsvFile(string filePath)
{
FileInfo file = new FileInfo(filePath);
//string connString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=\"" + file.DirectoryName + "\" Extended Properties='text;HDR=Yes;FMT=Delimited(,)';";
string connString = string.Format(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};" + @" Extended Properties={1}", file.DirectoryName, "'Text;HDR=YES;FMT=CSVDelimited'");
using (OleDbConnection con = new OleDbConnection(connString))
{
OleDbCommand cmd = new OleDbCommand(string.Format("SELECT * FROM [{0}]", file.Name), con);
// Using a DataTable to process the data
try
{
con.Open();
ds = new DataTable("MyTable");
OleDbDataAdapter adp = new OleDbDataAdapter(cmd);
adp.Fill(ds);
foreach (DataRow row in ds.Rows)
{
System.Diagnostics.Debug.WriteLine(row.ToString());
}
}
catch (Exception error)
{
String errorString;
errorString = "Error occurred while importing data from source file." + System.Environment.NewLine +
"Error Message: " + error.Message + System.Environment.NewLine +
"Stack Trace: " + error.StackTrace;
return errorString;
}
}
return "File imported to DataTable";
}