2014-03-12 199 views
-1

有誰能夠告訴我,爲什麼下面的代碼產生錯誤:附近有語法錯誤「(」

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 [email protected], [email protected], 
[email protected], [email protected], [email protected], 
[email protected], [email protected], 
[email protected], [email protected], 
[email protected] " 

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 
+4

因爲你混合了'insert'和'update'語法。 –

+0

您的意思是VALUES方式只能用於INSERT命令嗎? –

+0

更新命令必須使用'SET = ,...',而插入可以使用VALUES語法 – Charleh

回答

4

因爲在第一個示例中,您在UPDATE命令中使用INSERT語法。在第二個示例中,您正在使用正確的UPDATE語法。

INSERTUPDATE的語法不同。你應該閱讀文檔,但一般:

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

有更多的方式可以使用它,但基本上就是這樣。

+0

非常感謝,我是VB.NET新手,這是我使用SQL Server的第一個程序。來自VB6和Access! –

+0

這不是一個VB.NET問題,但是,這是對SQL的誤解。另外:豎起大拇指從一開始就使用參數化查詢! –

+0

感謝您的投票指導:-)我必須說我正在使用vb.net,但缺乏控制陣列使生活變得比應該更難! –