开发者

inserting a floating point value with a culture where comma (,) is used for "floating point"

开发者 https://www.devze.com 2023-01-08 17:35 出处:网络
开发者_StackOverflowI have something like this: foo { a = 1, b = 2, c = 98,3 } I generate the insert query dynamically so end up with this:

开发者_StackOverflowI have something like this:

foo { a = 1, b = 2, c = 98,3 }

I generate the insert query dynamically so end up with this:

insert foos(a,b,c) (1, 2, 98,3)

anybody knows how to workaround this ?


Simple answer: don't use text to insert values in the first place. Use a parameterized SQL query.

This isn't limited to numbers - it's also particularly important for dates and times. Conceptually, you're not dealing with a "number with a comma in" - you're dealing with a number. SQL happens to be the way we transfer data between the database and the client, but parameterized SQL allows us to keep the values as values without a pointless and error-prone conversion to text in between. Finally, parameterized queries are highly important as a guard against SQL injection attacks when transferring text values.

Basically, separate out the idea of "values" (which go in parameters) and "SQL code" which stays in text.

Just reformatting existing SQL which contains values until it happens to work is a brittle solution at best.


When generating SQL strings always use .ToString(CultureInfo.InvariantCulture) to ensure that decimals are formatted correctly for TSQL.

Or, for preference, use parameterised queries as Jon Skeet suggests above.


Can you not just cast the value to something with a '.'?

Edit: using parameterized queries would be better. And a simple 'Replace' would help as well.

0

精彩评论

暂无评论...
验证码 换一张
取 消