我使用以下语句作为参数。
.Parameters("@SearchValue").Value = TextBoxParentID.Text
此查询:
objSqlCommand.CommandText = "Select ID " & _
"From Parents " & _
"Where ID = @SearchValue"
ID是SQL服务器数字列但我收到此错误:
error converting nvarchar to numeric
你能告诉我需要修改的代码吗?
答案 0 :(得分:1)
此时您正在尝试分配字符串类型的值,并且参数绑定为字符串(nvarchar)。你需要先转换它。
' Let's suppose that this is a value from your textbox
Dim strNumber As String = "12"
' Prepare a placeholder to convert your string value into integer
Dim myInteger As Integer
' Try to convert
If Int32.TryParse(strNumber, myInteger) Then
'If the conversion is successful
Dim myConnection As New SqlConnection("Your ConnectionString")
Dim myCommand As New SqlCommand("INSERT INTO Foo(Bar) VALUES (@Bar", myConnection)
'Use this syntax to assign values to parameters
myCommand.Parameters.AddWithValue("Bar", myInteger)
End If
此代码段也使用AddWithValue
方法。我建议你使用这种方法。