VB.net中UPDATE语句中的语法错误

时间:2014-01-26 09:17:53

标签: sql vb.net syntax oledb

我有以下查询,我可以为爱而没有钱在我的UPDATE声明中找出错误的位置?我上周才开始玩这个游戏,所以我的学习曲线很大!

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
    'Declerations For Calling on
    Dim AnimalHouse As String
    AnimalHouse = "TestText"
    Dim AddressForAssingment As Integer
    AddressForAssingment = 1
    Dim IDCheckAssignment As Integer
    IDCheckAssignment = 1
    Dim CommandText As String = _
        "UPDATE IOInformation.Description = @animalHouse, WHERE ID_number = @addrForAssn AND ID_Check = @Id;"

    'Connection Information 
    Dim myConnection As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + ProjectDirectory.Text)
    Dim myCommand As New OleDbCommand(CommandText)
    myCommand.Parameters.AddWithValue("@animalHouse", AnimalHouse)
    myCommand.Parameters.AddWithValue("@addrForAssn", AddressForAssingment)
    myCommand.Parameters.AddWithValue("@Id", IDCheckAssignment)

    myCommand.Connection = myConnection
    myConnection.Open()
    myCommand.ExecuteNonQuery()
    myConnection.Close()


End Sub

3 个答案:

答案 0 :(得分:0)

您应该使用以下语法进行更新UPDATA tablename SET collumnName='value', columnname1='adsf' WHERE ...,不要','在哪里。 UPDATE IOInformation SET Description = ... WHERE ID_number = ... AND ID_Check = ...;

答案 1 :(得分:0)

您的语法中有几处错误。

UPDATE查询的正确语法是

UPDATE table SET field1=value1, field2=value2, ...... WHERE keyfield=value

所以你应该写

Dim CommandText As String = _
    "UPDATE IOInformation SET Description = @animalHouse " & 
    "WHERE ID_number = @addrForAssn AND ID_Check = @Id;"

将表名与字段名分开,在第一个字段名之前添加SET,并在WHERE之前删除逗号

答案 2 :(得分:0)

UPDATE IOInformation
SET Description = @animalHouse
WHERE  ID_number        = @addrForAssn
       AND ID_Check     = @Id

Bjørn-RogerKringsjå&史蒂夫在你的问题下面的评论中是正确的