Warm tip: This article is reproduced from serverfault.com, please click

Decimal number throwing exception during INSERT

发布于 2016-02-04 06:18:53

I have written an INSERT statement from inside a VB.NET application which inserts a bunch of data into a table which includes a Number column. MS access is the database. "Cycles" is the column in the table defined as of type "Number". I insert decimal values with two digits after decimal point for insertion into this column. The insert works all fine except from Germany. I realise that in Germany, they use "," (comma) instead of a "." (dot) for decimal point separation. Hence this application when run from a German windows machine fails at insert. Please help.

Questioner
Sameer
Viewed
0
Dai 2016-02-04 14:46:18

The formatting of numbers is a presentation concern, not a data persistence concern. If number-to-string formatting is affecting your SQL commands then it sounds like you're doing string concatenation to form a SQL command in a string. Do not do this.

Instead, use a parameterized query, that way the raw binary value of the number is sent to the database instead of a formatted string, like so:

Dim cmd As New SqlCommand("INSERT INTO Foo ( DecimalValue ) VALUES ( @value )")
cmd.Parameters.Add( "@value", SqlDbType.Decimal ).Value = someDecimal

Note the syntax for parameter placeholders depends on which database software you're using. Consult your system's SQL reference for specifics.

Note that if you want to ensure that a decimal number is formatted the same regardless of computer language settings do this:

Dim s As String
Dim d As Decimal = 2.0
s = d.ToString( CultureInfo.InvariantCulture )

I wish .NET had a built-in ToStringInvariant method to avoid needing to import System.Globalization.