user123_456
user123_456

Reputation: 5795

mysql is not accepting decimal in the right format

When I am sending decimal value from C# for example : 5.54 When I am executing query I can see that my variables are represented as 5,54

Anyone have an idea how to achieve to get DOT and not COMMA?

Here is my code:

using (MySqlConnection conn = new MySqlConnection(conn_string.ToString()))
                using (MySqlCommand cmd = conn.CreateCommand())
                {  
                    cmd.CommandText = string.Format("INSERT Test (lat, long) VALUES ({0},{1})",
                                                   OSGconv.deciLat, OSGconv.deciLon);
                    conn.Open();
                    cmd.ExecuteNonQuery();
                }

Upvotes: 1

Views: 1215

Answers (2)

JotaBe
JotaBe

Reputation: 39004

I totally agree with the other advice. As to the other question regarding comma or dot, it depends on the CurrentCulture. To get the right format you would have to use a ToString, providing Culture.InvariantCulture as the second parameter. This will use the dot for decimal separator.

But, I insist, the other answer is very good advice: use DbParameters. And I add: pass the value as held in C#, not converting it to string. It will be correctly handled by the ADO.NET provider. I.e. if you have to pass a float variable, do pass it without converting to string, but as is, as a float value.

Upvotes: 0

Jon Skeet
Jon Skeet

Reputation: 1499770

Anyone have an idea how to achieve to get DOT and not COMMA?

Don't embed the values into your SQL directly to start with. Use parameterized SQL instead. See the documentation for MySqlCommand.Parameters for an example. You should always use parameterized queries:

  • They separate code from data
  • They prevent SQL injection attacks (not an issue for these numbers, but consider strings...)
  • They prevent conversion issues like this

There's a more general point here: don't perform any more conversions than you have to. Every time you convert data from one form to another, you risk losing data or worse. If you can keep your data in the most appropriate representation for as long as possible, you'll minimize the problems.

Upvotes: 5

Related Questions