Note: answering my own problem to help others in future.
I'm reading a CSV file with OleDB:
DataTable csvTableSchema = new DataTable();
//Open the CSV
string csvFilePath = "C:\\temp\\WithDateColumn.csv";
var connString = string.Format(
@"Provider=Microsoft.Jet.OleDb.4.0; Data Source={0};Extended Properties=""Text;HDR=YES;FMT=Delimited""",
Path.GetDirectoryName(csvFilePath)
);
//To read the csv with DataTypes we specify the columns and their datatypes in the Schema.ini
//REF https://docs.microsoft.com/en-us/sql/odbc/microsoft/schema-ini-file-text-file-driver
using (var conn = new OleDbConnection(connString))
{
conn.Open();
var query = "SELECT * FROM [" + Path.GetFileName(csvFilePath) + "]";
using (var adapter = new OleDbDataAdapter(query, conn))
{
var ds = new DataSet("CSV File");
adapter.Fill(ds);
csvTableSchema = ds.Tables[0];
}
}
This is what the CSV files look like:
Here is a sample of the schema.ini
[ABC_20171128.csv]
Format = Delimited(|)
CharacterSet=ANSI
ColNameHeader=True
DateTimeFormat=dd MM yyyy hh:mm:ss
Col1 = Date DateTime
Col2 = AccountID Text
Col3 = SubAccount Text
Col4 = MarketCode Text
Col5 = SecurityCode Text
Col6 = Units Single
I've tried different syntaxs:
DateTimeFormat=dd MM yyyy hh:mm:ss
Fails wit this exception:
System.Data.OleDb.OleDbException: 'In the text file specification 'WithDateColumn.csv', the DateTimeFormat option is invalid.'
DateTimeFormat=dd MM yyyy
This syntax doesn't cause an error but the date column is empty.
How do I read DateTimes from a CSV using the Schema.ini file?