.net - C# OleDB CSV Import converting characters on its own -


i have csv document headers shown below:

date,time,tirca-501 [°c],pirca-501 [mpa],tirca-502 [°c],tirca-503 [°c],tir-504 [°c],wtria-501 [°c]

(the actual csv file more longer i've cut out relevant part)

here's utility method using parse csv file:

public static bool tryreadfromcsvfile(string csvfilepath, out datatable filecontent, bool isfirstrowheader) {     filecontent = new datatable();     try     {         string header = isfirstrowheader ? "yes" : "no";          string pathonly = path.getdirectoryname(csvfilepath);         string filename = path.getfilename(csvfilepath);          string sql = @"select * [" + filename + "] ";          using (oledbconnection connection = new oledbconnection(             string.format("provider=microsoft.jet.oledb.4.0;data source=\"{0}\";extended properties=\"text;characterset=65001;importmixedtypes=text;imex=1;hdr={1};fmt=delimited;typeguessrows=0\"",pathonly,header)))         using (oledbcommand command = new oledbcommand(sql, connection))         using (oledbdataadapter adapter = new oledbdataadapter(command))         {             filecontent.locale = cultureinfo.currentculture;             adapter.fill(filecontent);             return true;         }     }     catch (exception ex)     {         //logging utility here         return false;     } } 

the method works fine above data, square bracket '[' getting replaced regular bracket '(' in end result of parse.

just prove haven't lost sanity, here's proof (screenshots taken debugger):

enter image description here

enter image description here

i've checked hex code of problematic square bracket in original file. it's 5b, which denoted left square bracket in utf-8.

why oledb import cause this? how can prevent behavior?

edit: realize there exist many other ways of parsing csv files. heck, can read content list of strings , split commas. i'm trying understand why oledb causes issue can decide whether scrap utility method all-together or not. i'd see answer authoritative source on this.

i personal fan of microsoft.visualbasic.fileio.textfieldparser csv parsing. add reference microsoft.visualbasic. have saved header ansi encoded csv.

string datacsv;      using (var csvreader = new textfieldparser(     datacsv,      encoding.getencoding("iso-8859-1"),      true)) {     csvreader.textfieldtype = fieldtype.delimited;     csvreader.setdelimiters(",");      while (!csvreader.endofdata)     {         try         {             string[] currentrow = csvreader.readfields();             // turn datarow         }         catch (malformedlineexception ex) { }     }     // build datatable , add datarows  } 

Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

Rendering JButton to get the JCheckBox behavior in a JTable by using images does not update my table -