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. How can I fix it?
foreach (System.Data.DataRow row in dataTable.Rows) { System.Text.StringBuilder insertQuery = new System.Text.StringBuilder(); insertQuery.Append($"INSERT INTO {tableName} ");
codeSystem.Text.StringBuilder cols = new System.Text.StringBuilder(); foreach (System.Data.DataColumn column in dataTable.Columns) { cols.Append($"{column.ColumnName},"); } cols = cols.Remove(cols.Length - 1, 1); insertQuery.Append($"({cols}) "); System.Text.StringBuilder values = new System.Text.StringBuilder(); foreach (System.Data.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 is with the format of the double value in your CSV file. It seems like the value is using a comma (
,) instead of a period (.) for the decimal separator. In C#, the default decimal separator is a period (.), so when you try to insert a double value with a comma, it is not interpreted correctly.