Search code examples
c#.netwindowscsvodbc

While reading csv file, some fields are read along with commas. How can I avoid that?


While reading CSV data using C#, some field values are read along with the comma to seperate values. Like if I provide values like 7,8,18 instead of reading value 8 what I am getting is 8,.

This is how I am reading data

OdbcDataReader.GetValue(fieldNumber)

Here is my connection string

"Driver={Microsoft Text Driver (*.txt, *.csv)};dbg=;"

The problem here is its working in dev servers and not in higher environments. Suspected the file encoding. But tried editing the file in same editors. Still same result.

Any ideas or suggestions? Thanks in advance.


Solution

  • I wonder if the CurrentCulture is different on the dev server than it is on the production server. You could try temporarily changing to CultureInfo.InvariantCulture

    var currentCulture = System.Threading.Thread.CurrentThread.CurrentCulture;
    System.Threading.Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;
    
    OdbcDataReader.GetValue(fieldNumber)
    
    System.Threading.Thread.CurrentThread.CurrentCulture = currentCulture;