Missing column of Dates in CSV file using Schema.i

2020-04-21 03:35发布

问题:

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?

回答1:

Turns out OleDB uses nn instead of mm for minutes in DateTimes.

So the correct syntax is:

DateTimeFormat=dd MM yyyy hh:nn:ss

Ref stashed in the internet: http://forums.codeguru.com/showthread.php?333106-schema-ini-DateTimeFormat-legal-values

I couldn't see this in the official documentation: https://docs.microsoft.com/en-us/sql/odbc/microsoft/schema-ini-file-text-file-driver