0

我在表单上有一个文本框和一个按钮。

我希望运行一个查询(在 Vb.Net 中),它将产生一个带有 IN 值的查询。

下面是我的代码示例

myConnection = New SqlConnection("Data Source=sqldb\;Initial Catalog=Rec;Integrated Security=True")
myConnection.Open()
myCommand = New SqlCommand("UPDATE dbo.Recordings SET Status = 0 where RecID in ('" & txtRecID.Text & "') ", myConnection)
ra = myCommand.ExecuteNonQuery()
myConnection.Close()
MsgBox("Done!", _
MsgBoxStyle.Information, "Done")

当我输入单个值时它可以工作,但是当我用逗号输入值时它会引发错误:

“将 varchar 值 '1234,4567' 转换为数据类型 int 时转换失败。”

有人可以帮我解决这个问题,或者是否有其他方法?

非常感谢

4

2 回答 2

3

尝试删除包含 IN 值的单引号:

myCommand = New SqlCommand("UPDATE dbo.Recordings SET Status = 0 WHERE RecID IN (" & txtRecID.Text & ") ", myConnection) 
于 2010-09-30T10:34:52.140 回答
1

If you were testing a STRING variable for multiple values, then those multiple values would need to be in quotes.

The reason your first attempt 'almost' worked was because you could also have generated your SqlCommand with each individual value in quotes. I.E.
UPDATE dbo.Recordings SET Status = 0 where RecID IN ('1234', '5678')

In that case, T-SQL would have done an implicit conversion of each of the string values to INT, which is what it was attempting to do when you gave it '1234, 5678' but that isn't decipherable as an INT.

于 2011-03-28T12:59:53.487 回答