1

I am using the following code to read my csv file:

public 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.ACE.OLEDB.12.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);

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

        dAdapter.Dispose();

        return dTable;
    }

But the above doesn't reads the alphanumeric value from the csv file. it reads only i either numeric or alpha.

Whats the fix i need to make to read the alphanumeric values? Please suggest.

Prasad
  • 58,881
  • 64
  • 151
  • 199

4 Answers4

1

I suggest you use A Fast CSV Reader which does not have this issue and is much more faster.

Giorgi
  • 30,270
  • 13
  • 89
  • 125
0

Remove IMEX=1 from the connection string. I don't think you need it for CSV files.

anonymous
  • 3,474
  • 25
  • 29
  • Strange, I was doing something at work and used roughly the same code but with a different connection string for a comma-delimited file - Provider=Microsoft.Jet.OLEDB.4.0;Data Source=DirName;Extended Properties='text;HDR=Yes;FMT=Delimited' – anonymous Apr 13 '10 at 08:44
0

Try this OleDBAdapter Excel QA I posted via stack overflow.

I have not tried this out, but it sounds interesting! LinqToExcel they say it can be used on .CSV files as well...

Community
  • 1
  • 1
Brian Wells
  • 1,572
  • 1
  • 14
  • 12
0

hi all this code is gets alphanumeric values also

using System.Data.OleDb;

string ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + filepath + ";" + "Extended Properties="+(char)34+"Excel 8.0;IMEX=1;"+(char)34;

        string CommandText = "select * from [Sheet1$]";

        OleDbConnection myConnection = new OleDbConnection(ConnectionString);
        myConnection.Open();

        OleDbDataAdapter myAdapter = new OleDbDataAdapter(CommandText, myConnection);

        ds = null;
        ds = new DataSet();
        myAdapter.Fill(ds);
Marc Bernstein
  • 11,423
  • 5
  • 34
  • 32
balaji
  • 21
  • 2