Adrian
Adrian

Reputation: 13

Microsoft.Jet.OLEDB.4.0 weird issue reading csv file

I have a weird issue reading a .csv file using c#. Here are the file contents:

**ColumnName,                   Column Value**
Bestemming,                     aaa.asp
BuEn_SubSystem,                 B
BusinessListActive,             FALSE
cancreatelogin,                 0
CDTAccess,                      Y
CDTAssessmentNo,                4172
CDTAssessRO,                    N
CDTCntComp,                     0
CDTCntOnf,                      0
***CDTExpiry,                   1***

I am using the following code to read the file:

using System;
using System.Data;
using System.IO; //not used by default
using System.Data.OleDb; //not used by default


class CSVParser
    {
        public static DataTable ParseCSV(string path)
        {
            if (!File.Exists(path))
                return null;

            string full = Path.GetFullPath(path);
            string file = Path.GetFileName(full);
            string dir = Path.GetDirectoryName(full);


            //create the "database" connection string 
            string connString = "Provider=Microsoft.Jet.OLEDB.4.0;"
               + "Data Source=\"" + dir + "\\\";"
               + "Extended Properties=\"text;HDR=Yes;FMT=Delimited\"";

            //create the database query

            string query = "SELECT * FROM " + "[" + file + "]";

            //create a DataTable to hold the query results
            DataTable dTable = new DataTable();


            //create an OleDbDataAdapter to execute the query
            OleDbDataAdapter dAdapter = new OleDbDataAdapter(query, connString);

            try
            {
                //fill the DataTable
                dAdapter.Fill(dTable);                  

            }
            catch (InvalidOperationException /*e*/)
            { }

            dAdapter.Dispose();

            return dTable;
        }
    }

for some reason the CDTExpiry field is giving an issue. All I have to do is change the numeric to non-numeric for that field only and it works fine!! e.g CDTExpiry,1 to CDTExpiry,a.

Anyone know why this would be happening, as I am using this code for a bulk load of data from the csv file, and I don't want to always check the file first for this line of Code.

tx

Upvotes: 1

Views: 3069

Answers (1)

Purj0001
Purj0001

Reputation: 73

I believe there is an Extended Property in the Microsoft.Jet library that allows for something like this....I remember having a similar issue several years ago and I think I fixed it that way. Do some Googling about the extended properties and see if you can find one that will help your field accept a numeric

Upvotes: 1

Related Questions