'('附近的语法不正确

时间:2014-03-12 09:58:52

标签: sql vb.net visual-studio-2010 sql-server-2008

任何人都可以告诉我为什么以下代码会产生错误:

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

1 个答案:

答案 0 :(得分:4)

因为在您的第一个示例中,您在INSERT命令上使用UPDATE语法。在第二个示例中,您使用了正确的UPDATE语法。

INSERTUPDATE的语法不同。您应该阅读文档,但通常是:

INSERT INTO <Table> (Field1, ..., FieldN) VALUES (Value1, ..., ValueN)
vs.
UPDATE <Table> SET Field1 = Value1, ..., FieldN = ValueN WHERE <Condition>

还有更多方法可以使用它,但基本上就是它。