Single DataType and decimal separator

  • Thread starter Thread starter Mika M
  • Start date Start date
M

Mika M

Lets say I need to do something like this when running sql-sentences...

Private Sub InsertIntoTable(ByVal ProductID As Integer, ByVal Amount As
Single)
Dim sql as String = String.Format("INSERT INTO MyTable (ProductID,
Amount) VALUES ({0}, {1})", ProductID, Amount)
'...code continues...
End Sub

....in this case problem will be "Amount"- Single type variable, because
for example here in Finland our Decimal symbol is , (= comma) not . (=
point).

If amount is one and half (1.5) then our form is 1,5 and it's not
correct form for sql-sentences directly - so what's the recommended way
for these to convert into correct form? I have handled single numbers
like this way...

Dim strAmount As Single = Amount.ToString.Replace(",", ".")

....and then used strAmount like...

Dim sql as String = String.Format("INSERT INTO MyTable (ProductID,
Amount) VALUES ({0}, {1})", ProductID, strAmount)

....but I assume it's not good to do it like this way :)
 
Mika M said:
Lets say I need to do something like this when running sql-sentences...

Private Sub InsertIntoTable(ByVal ProductID As Integer, ByVal Amount As
Single)
Dim sql as String = String.Format("INSERT INTO MyTable (ProductID,
Amount) VALUES ({0}, {1})", ProductID, Amount)
'...code continues...

Instead of building the command strings yourself, use a parameterized
command object ('SqlCommand.Parameters').
 
Mika,

In addition to Herfried,

Be aware that in the case of the parameters OleDb and SQLClient have
different behaviour.

Therefore look very good which you use.
OleDbCommand.Parameters <> SQLCommandParameters etc.

I hope this helps,

Cor
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top