VB.NET/SQL Server更新语句出错

时间:2013-04-28 05:49:21

标签: sql sql-server vb.net

背景:我在Visual Studio 2010中有一个带有VB.NET代码的Windows窗体。在表单上我有一个组合框,我可以从我的SQL表中选择一个记录。选择完成后,将使用我的SQL表数据填充几个文本框。

问题:我需要编辑转储到这些文本框中的信息。通过编辑,我的意思是我需要更改Windows窗体上文本框的内容,单击我的更新按钮,并更新我的SQL表的内容。

这是我的代码:

    Dim con As New SqlConnection
    Dim conDim cmd As New SqlCommand
    Try
        con.ConnectionString = "Server=fakeservername; Database=fakedatabasename; integrated security=true"
        con.Open()
        cmd.Connection = con
        cmd.CommandText = ("UPDATE Users " & _
                           "SET User_FName = '" & Trim(txtFName.Text) & "'," & _
                           "User_LName = '" & Trim(txtLName.Text) & "' ," & _
                           "User_Address = '" & Trim(txtAddress.Text) & "'," & _
                           "User_City = '" & Trim(txtCity.Text) & "'," & _
                           "User_State = '" & Trim(txtState.Text) & "'," & _
                           "User_Zip = '" & Trim(txtZip.Text) & "'," & _
                           "User_Phone = '" & Trim(txtPhone.Text) & "'," & _
                           "User_AltPhone = '" & Trim(txtAltPhone.Text) & "'," & _
                           "WHERE Users.User_ID ='" & (txtUserID.Text) & "';")
        cmd.ExecuteNonQuery()
    Catch ex As Exception
        MessageBox.Show("Error while inserting record on table..." & ex.Message, "Insert Records")
    Finally
        con.Close()
    End Try

这是我的错误:

”'Where'附近的语法不正确。

我在互联网上寻找这个问题的答案。我想我会在答案中小心翼翼,但我无法指责它。所以,我来找你们。有什么建议?提前谢谢!

1 个答案:

答案 0 :(得分:2)

where条件之前的最后一列中还有其他逗号。

"User_AltPhone = '" & Trim(txtAltPhone.Text) & "',"

应该是

"User_AltPhone = '" & Trim(txtAltPhone.Text) & "'"