我意識到這已被問了一百萬次,但我找不到有效的答案。新的SQL服務器和vs 2010 vb(抱怨老闆,而不是我),做練習運行。不斷地得到一個VARCHAR到數值異常,如下所示:varchar數值異常
Dim height, width As Decimal
height = InputBox("Enter height:")
width = InputBox("Enter width:")
'PRODUCING VARCHAR TO NUMERIC EXCEPTION
command = New SqlCommand("DECLARE @height as decimal(10,2), " & _
"@width as decimal(10,2), " & _
"@area as decimal(10,2); " & _
"SET @height = ' " & height & " '; " & _
"SET @width = ' " & width & " '; " & _
"SET @area = @height * @width; " & _
"IF @height IS NULL OR @width IS NULL " & _
" SELECT 'Either the height or width field was blank.' " & _
"ELSE IF @height <= 0 OR @width <= 0 " & _
" SELECT 'That''s some fancy negative space.' " & _
"ELSE " & _
" SELECT 'The area is: ' [email protected];" _
, connect)
connect.Open()
rdr = command.ExecuteReader()
我已經通過驗證VARTYPE()中傳遞的數據確實是十進制(我是唯一一個測試,所以並不擔心誤差控制ATM )。我曾嘗試使用帶和不帶單引號的變量。 Cast()和Convert()不起作用(除非我做錯了嗎?)。不知道問題出在哪裏?
請勿使用字符串替換來構建sql。它讓你面對一些非常惡劣的安全問題。 –
爲什麼要進入數據庫,當你可以在vb.net中做同樣的事情? – shahkalpesh
檢查語言環境,'10.0'和'10,0'和小數點分隔符 – lad2025