我有以下SQL查询在SQL服务器管理中有效:
Update SQLTableBlokke
set blokgemiddeld = ((tha_min4 + tha_min3 + tha_min2 +
tha_min1 + tha_huidig) /
NULLIF(((ABS(sign(tha_min4))+ABS(sign(tha_min3))+ABS(sign(tha_min2))+
ABS(sign(tha_min1))+ABS(sign(tha_huidig))) * 1.00),0))
作为初学者,我无法将此命令用作vb.net命令。我尝试过以下方法:
Dim konneksie As New SqlConnection
Dim opdraggem As New SqlCommand
konneksie.ConnectionString = "Data Source=GIDEON-E- LAPTOP\SQLEXPRESS2014;Initial Catalog=BlokwinsgewendheidDatabasis;Integrated Security=True"
konneksie.Open()
opdraggem.Connection = konneksie
opdraggem.CommandText = "Update(SQLTableBlokke)" & _
"blokgemiddeld = @((tha_min4 + tha_min3 + tha_min2 + tha_min1 + tha_huidig) / " & _
" NULLIF(((ABS(sign(tha_min4)) + ABS(sign(tha_min3)) + ABS(sign(tha_min2)) + ABS(sign(tha_min1)) + ABS(sign(tha_huidig))) * 1.0), 0)) "
opdraggem.ExecuteNonQuery()
但是我收到错误消息:附加信息:'附近的语法不正确('。光标停在opdraggem.ExecuteNonQuery()行。
我想我必须使用参数,但不知道如何实现它们。
非常感谢任何对新手的帮助。
答案 0 :(得分:0)
在这种情况下,我不认为您需要使用参数,因为您没有使用表中尚未包含的任何数据进行更新。你的CommandText应该是:
opdraggem.CommandText = "Update SQLTableBlokke" & _
"set blokgemiddeld = ((tha_min4 + tha_min3 + tha_min2 + " & _
"tha_min1 + tha_huidig) / " & _
"NULLIF(((ABS(sign(tha_min4))+ABS(sign(tha_min3))+ABS(sign(tha_min2))+ " & _
"ABS(sign(tha_min1))+ABS(sign(tha_huidig))) * 1.00),0))"
您的代码也将受益于连接上的using语句,因为它将确保连接始终关闭。在这里展示: https://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand.executenonquery(v=vs.110).aspx
答案 1 :(得分:0)
谢谢大家。是的,我错过了关键字“Set”。 此致