INSERT of a Decimal using VB.NET

  • Thread starter Thread starter DraguVaso
  • Start date Start date
D

DraguVaso

Hi,

I want to insert some recors in an Sql Server which have some decimal
values:

For exemple: I have a vairable called sngPi = 3,1415 (european notation:
"," = comma)

When I build my SQL-statement dynamically like:
strSQL = "INSERT INTO tblBlabla (MyName, Pi) VALUES ('Pieter', " & sngPi &
")"

But: the result is offcourse: "INSERT INTO tblBlabla (MyName, Pi) VALUES
('Pieter', 3,1415)" which gives me an error that I want to insert 2 values,
and have only one field.

So I actually should insert it as ""INSERT INTO tblBlabla (MyName, Pi)
VALUES ('Pieter', 3.1415)" to work fine.

I could 'build' my variable as a string by taking the first part,
concattenating a point, and than concatenate the last part, but I guess
there must be a nicer and better way to work arround this problem. I want a
solution that works in every case, independent of the regional settings on
the application-pc and the sql-server.

Anybody any idea?

Thansk a lot in advance!

Pieter
 
DraguVaso said:
When I build my SQL-statement dynamically like:
strSQL = "INSERT INTO tblBlabla (MyName, Pi) VALUES ('Pieter', " &
sngPi & ")"

But: the result is offcourse: "INSERT INTO tblBlabla (MyName, Pi)
VALUES ('Pieter', 3,1415)" which gives me an error that I want to
insert 2 values, and have only one field.

So I actually should insert it as ""INSERT INTO tblBlabla (MyName,
Pi) VALUES ('Pieter', 3.1415)" to work fine.

Use the Parameters property of the SQLCommand object.
 
Use a data adapter or parameterized sql command object, it will take care of
the conversion for you.
 
This may not be an ideal solution but here's one way you could do it:
sngPi.ToString.Replace(",", ".")
 
Back
Top