I have to read a CSV (That is opened as Excel) file and store the records in database. What I am doing is first read all lines in to a string array and load data to a DataTable and then push it to database. Some fields have commas in them and since I split fields using the comma itself, this is causing trouble. CSV is what I get from customers and I have no authority over asking them to format it correctly. Any idea how to work around this?
code:
Dim dt As New DataTable
Dim fields() As String
Dim lines() As String = File.ReadAllLines(fileName)
'Create headers
lines(1) = lines(1).Replace(Chr(34), "")
For Each header As String In lines(1).Split(",")
dt.Columns.Add(header)
Next
'Fill data
For i As Integer = 1 To lines.Count() - 1
fields = lines(i).Split(",")
dt.Rows.Add(fields)
Next
sample data row:
540,TestName,**$2,136.02**,0.15%,8004310/01
So this breaks in to 6 values where as it should be 5