更新表时“条件表达式中的数据类型不匹配”错误

时间:2016-09-18 12:46:43

标签: sql vb.net ms-access

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    Dim con As New OleDb.OleDbConnection
    Dim str As String = "Provider=Microsoft.ACE.OLEDB.12.0; Data Source=..\VisitorPass.accdb"
    Dim sql As String
    sql = "Update Visitor set [password]='" & txtPassword.Text & "',FirstName='" & txtFirstName.Text & "',LastName='" & txtLastName.Text & "',Gender='" & txtGender.Text & "',MobileNo='" & txtMobileNO.Text & "',DateOfBirth='" & txtDateOfBirth.Text & "',VisitorAddress='" & txtVisitorAddress.Text & "'where ID='" & lblID2.Text & "'"
    con = New OleDbConnection(str)
    Dim cmd As New OleDbCommand(sql, con)
    con.Open()
    cmd.ExecuteNonQuery()
    Dim obj1 As New VisitorProfile
    obj1.StringPass = txtName.Text
    MsgBox("profile is updated")
    obj1.Show()
    con.Close()
    Me.Close()

End Sub

1 个答案:

答案 0 :(得分:0)

您的条件表达式中只有一个字段Id。我怀疑它不是Text而是它实际上是一个Integer。所以删除引号:(还在单引号和其中的位置添加空格。)

"' where ID=" & lblID2.Text & "

然而,这会让您对sql注入攻击开放,因此您应该对查询进行参数化。