When reading a CSV file using a DataReader and the OLEDB Jet data provider, how can I control column data types?

前端 未结 4 1277
难免孤独
难免孤独 2020-11-30 06:40

In my C# application I am using the Microsoft Jet OLEDB data provider to read a CSV file. The connection string looks like this:

Provider=Microsoft.Jet.OLEDB         


        
4条回答
  •  孤城傲影
    2020-11-30 07:22

    To expand on Marc's answer, I need to create a text file called Schema.ini and put it in the same directory as the CSV file. As well as column types, this file can specify the file format, date time format, regional settings, and the column names if they're not included in the file.

    To make the example I gave in the question work, the Schema file should look like this:

    [Data.csv]
    ColNameHeader=True
    Col1=House Text
    Col2=Street Text
    Col3=Town Text
    

    I could also try this to make the data provider examine all the rows in the file before it tries to guess the data types:

    [Data.csv]
    ColNameHeader=true
    MaxScanRows=0
    

    In real life, my application imports data from files with dynamic names, so I have to create a Schema.ini file on the fly and write it to the same directory as the CSV file before I open my connection.

    Further details can be found here - http://msdn.microsoft.com/en-us/library/ms709353(VS.85).aspx - or by searching the MSDN Library for "Schema.ini file".

提交回复
热议问题