I am currently working on a class project to produce a CSV reader in C#. I encountered errors when trying to read the data and read previous posts about trying to override the MaxScanRows
task. I choose to incorporate a schema writing to the program that would write a schema at the point of reading the csv to a data grid view.
When trying to debug the code the console outputs this:
'TSP GUI.exe' (CLR v4.0.30319: TSP GUI.exe): Loaded 'C:\WINDOWS\Microsoft.Net\assembly\GAC_32\System.Transactions\v4.0_4.0.0.0__b77a5c561934e089\System.Transactions.dll'.
Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
Exception thrown: 'System.Data.OleDb.OleDbException' in System.Data.dll
The code is:
public DataTable ReadCsv(string csvFileName)
{
DataTable dt = new DataTable();
try
{
using (OleDbConnection conn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=\"" +
Path.GetDirectoryName(csvFileName) + "\";Extended Properties='text;HDR=Yes;IMEX=1;FMT=Delimited(,)';"))
{
using (OleDbCommand cmd = new OleDbCommand(string.Format("select *from [{0}]", new FileInfo(csvFileName).Name), conn))
{
conn.Open();
using (OleDbDataAdapter obj_oledb_da = new OleDbDataAdapter(cmd))
{
DataTable dtSchema = new DataTable();
obj_oledb_da.FillSchema(dtSchema, SchemaType.Source);
if (dtSchema != null)
{
writeSchema(dtSchema);
}
obj_oledb_da.Fill(dt);
}
}
}
}
catch
{
MessageBox.Show("Error occured");
}
return dt;
}
The exception is thrown when conn.open();
runs.
Thanks for editing and passing direction to me,
Another option that will fix my problem is the extended properties for the connection string. I just dont know what to put to allow a CSV to be read with all rows as the csv file has mostly numerics in a row but rarely has a combination of Alphanumerics.