无法使用OLEDB更新数据库中的记录

时间:2016-02-09 06:22:01

标签: sql asp.net vb.net ms-access

我正在创建一个程序,我想在其中更新管理员的密码。我尝试了SELECT UPDATE语句,但仍然给我一个错误。我将在后面显示代码片段。

Dim con As OleDb.OleDbConnection
Dim cmd As OleDb.OleDbCommand
Dim dr As OleDb.OleDbDataReader
Dim selectQuery As String
Dim updateQuery As String

Protected Sub Reset Password
    If txtNewPassword.Text <> txtRepeatPassword.Text Then
        Response.Write(<script>alert('Password dont match!')</script>)
        Response.Write("<script>window.location.reload()</script>")
    Else
        Using con As New OleDb.OleDbConnection("Provider=Microsoft.JET.OLEDB.4.0;data source = " & Server.MapPath("Point_Of_Sales.mdb"))
            con.Open()
            selectQuery = "SELECT * FROM Admin WHERE username = '" & Request.QueryString("userName") & "'"
            Using cmd As New OleDb.OleDbCommand(selectQuery, con)
                dr = cmd.ExecuteReader
                cmd.Dispose()
                con.Close()
            End Using
        End Using

        Using con As New OleDb.OleDbConnection("Provider=Microsoft.JET.OLEDB.4.0;data source = " & Server.MapPath("Point_Of_Sales.mdb"))
            con.Open()
            updateQuery = "UPDATE Admin SET (password = @repeatNewPass) WHERE (username = @username) "
            Using cmd As New OleDb.OleDbCommand(updateQuery, con)
                cmd.Parameters.AddWithValue("@repeatNewPass", txtRepeatPassword.Text)
                cmd.Parameters.AddWithValue("@username",Request.QueryString("userName")
                cmd.ExecuteNonQuery()
                cmd.Dispose()
                con.Close()
            End Using
            Response.Write(<script>alert('Successfully updated the password for admin!')</script>)
        End Using
    End If
End Sub

- 正如您所看到的,我使用Request.QueryString从另一个页面检索值,即userName。我遇到的错误是在具有UPDATE查询的第二个连接中。任何帮助和建议都会有所帮助。

1 个答案:

答案 0 :(得分:0)

密码是保留字,请尝试:

updateQuery = "UPDATE Admin SET ([password] = @repeatNewPass) WHERE (username = @username) "

和参数刚刚排序:

updateQuery = "UPDATE Admin SET ([password] = ?) WHERE (username = ?) "