Cannot read all columns from csv using oledb

I have a csv file with the following heading:    "Dispatch date" , "Shipment time", "Departure address", "From zone" , etc. I can read only the first 2 columns and nothing but using oledb. I used the schema.ini file with all the specified column names. Pls offers.

Here is my csv example.

"PickupDate","PickupTime","PickupAddress","FromZone"
"11/05/15","4:00:00 AM","9 Houston Rd, CityName, NC 28262,","262"

Here is my code:

Schema.ini
-----------
[ReportResults.csv]
ColNameHeader = True
Format = CSVDelimited
col1=Pickup Date DateTime
col2=Pickup Time Text width 100
col3=Pickup Address Text width 500
col4=FromZone short

oledb code
-----------      

     public static DataTable SelectCSV(string path, string query)
     {
      // since the file contains addresses with , the delimiter ", is used. Each cell is written within "" in  the file.
                  var strConn = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + path +
                                "; Extended Properties='text;HDR=Yes;FMT=Delimited(\",)'";

                  OleDbConnection selectConnection = (OleDbConnection)null;
                  OleDbDataAdapter oleDbDataAdapter = (OleDbDataAdapter)null;


                      selectConnection = new OleDbConnection(strConn);
                      selectConnection.Open();
                      using(OleDbCommand cmd=new OleDbCommand(query,selectConnection))
                      using (oleDbDataAdapter = new OleDbDataAdapter(cmd))
                      {
                          DataTable dt = new DataTable();
                          dt.Locale=CultureInfo.CurrentCulture;
                          oleDbDataAdapter.Fill(dt);
                          return dt;
                      }
    }
+4
source share
1 answer

Each column is enclosed in double quotes, so each comma inside the double quotation mark is not considered a divisor.

So you can import the file:

  • without use schema.ini
  • EXTENDED PROPERTIES='text;HDR=Yes;FMT=Delimited'

, , schema.ini ; - :

[ReportResults.csv]
ColNameHeader = True
Format = CSVDelimited
col1=PickupDate DateTime
col2=PickupTime Text width 100
col3=PickupAddress Text width 500
col4=FromZone short

DateTime, DateTimeFormat options; .. 2015/11/13, DateTimeFormat=yyyy/MM/dd=yyyy/MM/dd.

Short, , FromZone -32768 32767; , . DecimalSymbol, .

MSDN.

+1

Source: https://habr.com/ru/post/1615722/


All Articles