查询Access时如何避免保留字出现错误?

How can I avoid errors around reserved words when querying Access?

我使用下面的代码将用户登录到我的系统:

Private Sub btnLogin_Click(sender As Object, e As EventArgs) Handles btnLogin.Click

    Dim LogUsername As String = txtUsername.Text
    Dim LogPassword As String = txtPassword.Text

    If txtPassword.Text <> "" And txtUsername.Text <> "" Then
        cmdLogin.CommandText = "SELECT * FROM [User Query] WHERE Username ='" & CStr(LogUsername) & "' AND Password ='" & CStr(LogPassword) & "'"
        cmdLogin.Connection = cnnOledb
        Dim DR As OleDbDataReader = cmdLogin.ExecuteReader()

        If DR.Read = True Then

            If DR(0).ToString = LogUsername And DR(1).ToString = LogPassword Then

                'User_ID = DR(2).ToString
                MsgBox("Login Succesful!")
            End If

        Else : MsgBox("Invalid Username or Password, please try again", MsgBoxStyle.Critical + MsgBoxStyle.RetryCancel, "Login Error")
            txtUsername.Clear()
            txtPassword.Text = ""

        End If

        DR.Close()
    End If
End Sub

点击登录按钮后,抛出异常:

The SELECT statement includes a reserved word or an argument name that is misspelled or missing, or the punctuation is incorrect

这是我点击登录按钮时出现错误的截图。

这是我用来登录的 table 的屏幕截图。

我同意这些评论,但你的问题的直接原因是 "Password" 是 Access 中的保留字,所以如果你想将它用作标识符,你需要将它转义。您已经知道如何做到这一点,因为您已经在使用 table 名称进行操作,而您在其中用 space 命名的名称很糟糕。