Feb 5, 2013

Representing proper T-SQL string of .NET float & double types

You build a dynamic T-SQL statement in C#.
There is .NET double variable.
You need correct culture independent version of your double variable.
In many European cultures decimal separator is comma and not dot.

So when you pick double value from app.GUI defined by client culture it will be:
1,01

If you inject this in T-SQL statement it is of course invalid.
To workaround this  use InvariantCulture.


var result = 1E-1;
string MysqlDoubleString = Convert.ToString(result, System.Globalization.CultureInfo.InvariantCulture);

Please note that above scenario of building dynamic T-SQL is very risky and unsecure since it is prone to SQL injection attacks.

Always use classic ADO.NET SQLParameter class instead.

No comments:

Post a Comment