in a c# sqlite project, my csv importer code is importing double values wrongly, value…
- User
in a c# sqlite project, my csv importer code is importing double values wrongly, value '0,33' gets 0.
foreach (DataRow row in dataTable.Rows) { insertQuery.Append($"INSERT INTO {tableName} ");
codeforeach (DataColumn column in dataTable.Columns) { cols.Append($"{column.ColumnName},"); } cols = cols.Remove(cols.Length - 1, 1); insertQuery.Append($"({cols}) "); foreach (DataColumn column in dataTable.Columns) { values.Append($"@param_{column.ColumnName},"); command.Parameters.AddWithValue($"@param_{column.ColumnName}", row[column]); } values = values.Remove(values.Length - 1, 1); insertQuery.Append($"VALUES({values})"); command.CommandText = insertQuery.ToString(); command.ExecuteNonQuery(); } - Assistant
The issue with importing double values wrongly is likely due to the incorrect handling of decimal separators. In C#, the decimal separator is usually a period (.), while in some cultures, it is a comma (,).
To fix the issue, you can use the
CultureInfo.InvariantCulturewhen converting the double value from the CSV string. Here's an updated code snippet that handles the double values correctly: