SQL中的UPDATE命令的语法错误

时间:2014-12-17 12:42:38

标签: sql vb.net ms-access syntax

这句话有什么不妥吗?我找不到任何问题......

UPDATE AccountInfo
SET First Name = 'Test', Last Name = 'Account', Street = 'Street', State = 'State', ZipCode = 55555
WHERE id = 1

我正在编写VB程序,并且我正在实现编辑功能。这是问题代码。

        Public Function updateAccountInfo(ByVal cp As CPerson, ByVal fName As String, ByVal lName As String, ByVal address As String, ByVal state As String, ByVal zip As Integer) As Boolean
    Dim sql, sql2 As String
    sql = "UPDATE AccountInfo SET First Name = '" & fName & "', Last Name = '" & lName & "', Street = '" & address & _
        "', State = '" & state & "', ZipCode = " & zip & " WHERE id = " & cp.returnIDOnlyNumber
    sql2 = "UPDATE Accounts SET First Name = '" & fName & "', Last Name = '" & lName & "' WHERE id = " & cp.returnIDOnlyNumber
    Return do_command(sql, sql2)
End Function

Private Function do_command(ByVal sql As String, ByVal sql2 As String) As Boolean
        Dim command As OleDbCommand
        Try
            conn.Open()
            command = New OleDbCommand(sql, conn)
            command.ExecuteNonQuery()
            conn.Close()
            conn.Open()
            command = New OleDbCommand(sql2, conn)
            command.ExecuteNonQuery()
            conn.Close()
            Return True
        Catch ex As Exception
            Return False
        End Try
    End Function

1 个答案:

答案 0 :(得分:5)

是的,名字中有空格。如果你真的拥有它们,那么你需要逃脱它们,也许是用方括号:

UPDATE AccountInfo
    SET [First Name] = 'Test', 
        [Last Name] = 'Account',
        Street = 'Street',
        State = 'State',
        ZipCode = '55555'
WHERE id = 1;

最好让名字没有空格。此外,邮政编码应存储为字符串,而不是数字,否则您将丢失前导零。