Вопрос

From what I can ascertain, most SQL implementations insist on a '.' decimal point in numeric literals.

Do any use a ',' instead? (ie. as used by many European countries), or can the '.' be assumed?

The specific situation I have, has .NET/ADO.NET calling a database through ODBC. For reasons I won't go into, UPDATE statements are being sent as SQL strings. .NET is being clever with its localization and using the decimal point specified in the user's localization settings. This can lead to SQL syntax errors. It is easy enough to fix in the specific case (force English style decimal points), but I want to make sure it will work in all cases, or at least be aware of the cases when it won't work.

I thought this might be a setting in the ADO.NET connection config, but I haven't been able to find anything.

Это было полезно?

Решение

Since the comma is used for separate thing in SQL, I doubt that any SQL system allow it.

Back to your question, of course is better to use ADO.NET with parameters to avoid the problems, but if you are forced to concatenate the string use Format and CultureInvariant to get an invariant string from your number.

This extension method should be handy

public static class DecimalHelpers {
    public static string ToInvariantString(this Decimal Numero) {
        return Numero.ToString("0.####", CultureInfo.InvariantCulture);
    }
}

I strongly recommend you against string concatenation, it's a major PITA and can be source of SQL injection attacks and other problems. It's 2012 and we are beyond that.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top