Ok, this started out as a pretty simple task to import a customers data into our database. The script below loads all the data into memory and then allow manipulation after.
string ConString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=\"" + FileLoc + "\"; Extended Properties=\"text;HDR=Yes;FMT=Delimited(,)\"";
using (OleDbConnection conn = new OleDbConnection(ConString))
{
using (OleDbCommand cmd = new OleDbCommand("SELECT * FROM DataInput.csv", conn))
{
conn.Open();
using (OleDbDataReader dr = cmd.ExecuteReader(CommandBehavior.SequentialAccess))
{
int RowNumber = 0;
while (dr.Read())
{
if(RowNumber==0)
{
for (int i = 0; i < dr.FieldCount; i++)
{
TitleData.Add(new CSVTitleData() {ColumnID = i,ColumnName = dr.GetValue(i).ToString()});
}
}
else
{
var DataToInsert = new List<CSVData>();
for (int i = 0; i < dr.FieldCount; i++)
{
if (i == 8)
{
var A = dr.GetValue(i);
Console.WriteLine(A.GetType().ToString() + " - " + i);
}
DataToInsert.Add(new CSVData(){ColumnID = i,Value = dr.GetValue(i).ToString()});
}
Content.Add(DataToInsert);
}
RowNumber++;
}
}
}
}
This works fine until there is data like
4950
4951
4952
4998
2024
4176
7025-1
RVAC010
RVAC011
4945
4946
4947
4948
Excel tries to be clever and reads each of the fields as an Int. Resulting int the ones with a letters in being read as DBNull.
Now I’ve done some reading and there are lots of people with this question but no solid solution. One of the possible solutions is to create a Scheme.ini I wonder if someone can help me out with the exact format of that?
Or perhaps there is an easier was of reading in the CSV?
I’ve had the same experience with this…. I found fantastic library at codeproject for reading CSV files:
http://www.codeproject.com/KB/database/CsvReader.aspx
The library handles everything such as parsing CSVs correctly so you don’t have to worry about problems like these. It is very easy to use and the data can be returned via a DataTable.
It has become an essential part of my code library I highly recommend you check it out.