I'm trying to add records from a CSV file into a dataTable in vb.net, as 开发者_StackOverflow社区follows:
dim myObjAdapter as new myDataSourceAdapters.myTableAdapter
dim myObjTable as new myDataSource.myTable
myObjTable = myObjAdapter.Getdata
'The problem is not in csvReader, I've printed the data in Debug and the numbers is ok.
Dim csvR As New LumenWorks.Framework.IO.Csv.CsvReader(New StreamReader("c:\myFile.csv"), True)
do While csvR.NextRecord 'method nextrecord returns true if a record exists and moves forward
'Class csvreader returns the value of the specified column
myObjTable.AddMyTableRow(csvr("ID"),csvr("rate"))
loop
myObjAdapter.update(myObjTable)
This produces for a csv file containing "1,0.5414" a record [1|5414] instead [1|0.5414] in table.
When I read the table, all the decimal values are converted to integer. I supose that it occours due to my regional settings, where decimal separator is (",") and the digit grouping symbol is ("."). Then, the addRow method from table is something like parsing the semicolons as the settings in region configuration. I can bypass this problem using:
myObjTable.AddMyTableRow(csvr("ID"),cdbl(csvr("rate").Replace(".",",")))
but I don't think that is the "smartest" solution.
PS: in CSV file, the decimal digit is "." and if I add directly a record into table, using "." as decimal separators, the numbers get right format.
There is a kind of settings to change, or option to enable/disable on adding this records to do the right adding?
Use InvariantCulture when parsing the numbers
For example
Dim s as new string = "1,0.3434"
Dim InvC As CultureInfo = CultureInfo.InvariantCulture
dim invariantString as string = s.ToString(InvC)
精彩评论