任何人都可以告诉我为什么以下代码会产生错误:
cQueryIS = "UPDATE Additional (StaffEUPN, Class, Additional1, Additional2, Additional3, Additional4,AdditionalLarge1, AdditionalLarge2, AdditionalLarge3, AdditionalLarge4) "
cQueryIS &= "VALUES (@cSTEupn, @cClass, @cAdditional1, @cAdditional2, @cAdditional3, @cAdditional4, @cAdditionalLarge1, @cAdditionalLarge2, @cAdditionalLarge3, @cAdditionalLarge4) "
cQueryIS &= "WHERE StaffEUPN = '" & cStaffEUPN & "' AND Class = '" & cClass & "'"
但是这段代码有效:
cQueryIS = "UPDATE Additional SET StaffEUPN=@cSTEupn, Class=@cClass,
Additional1=@cAdditional1, Additional2=@cAdditional2, Additional3=@cAdditional3,
Additional4=@cAdditional4, AdditionalLarge1=@cAdditionalLarge1,
AdditionalLarge2=@cAdditionalLarge2, AdditionalLarge3=@cAdditionalLarge3,
AdditionalLarge4=@cAdditionalLarge4 "
cQueryIS &= "WHERE StaffEUPN = '" & cStaffEUPN & "' AND Class = '"
& cClass & "'"
我有点困惑,因为我的方式首先在INSERT
命令上正常工作,只会导致UPDATE
上的错误。
以下是代码的其余部分:
Using dbConnectionSQL As New SqlConnection(dbConnectionString)
Using comm As New SqlCommand()
With comm
.Connection = dbConnectionSQL
.CommandType = CommandType.Text
.CommandText = cQueryIS
.Parameters.AddWithValue("@cSTEupn", cStaffEUPN)
.Parameters.AddWithValue("@cClass", cClass)
.Parameters.AddWithValue("@cAdditional1", ProvisionAdditional(1))
.Parameters.AddWithValue("@cAdditional2", ProvisionAdditional(2))
.Parameters.AddWithValue("@cAdditional3", ProvisionAdditional(3))
.Parameters.AddWithValue("@cAdditional4", ProvisionAdditional(4))
.Parameters.AddWithValue("@cAdditionalLarge1", ProvisionAdditional(5))
.Parameters.AddWithValue("@cAdditionalLarge2", ProvisionAdditional(6))
.Parameters.AddWithValue("@cAdditionalLarge3", ProvisionAdditional(7))
.Parameters.AddWithValue("@cAdditionalLarge4", ProvisionAdditional(8))
End With
Try
dbConnectionSQL.Open()
comm.ExecuteNonQuery()
Catch ex As SqlException
WriteToLogFile("Update Additional", ex.Message.ToString())
MsgBox("Update Additional - " & ex.Message.ToString())
Exit Sub
End Try
End Using
End Using
答案 0 :(得分:4)
因为在您的第一个示例中,您在INSERT
命令上使用UPDATE
语法。在第二个示例中,您使用了正确的UPDATE
语法。
INSERT
和UPDATE
的语法不同。您应该阅读文档,但通常是:
INSERT INTO <Table> (Field1, ..., FieldN) VALUES (Value1, ..., ValueN)
vs.
UPDATE <Table> SET Field1 = Value1, ..., FieldN = ValueN WHERE <Condition>
还有更多方法可以使用它,但基本上就是它。