我使用下面的代码将用户登录到我的系统中:
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
点击登录按钮后,会抛出异常:
SELECT语句包含拼写错误或缺失的保留字或参数名称,或者标点符号不正确
答案 0 :(得分:1)
我同意这些评论,但问题的直接原因是“密码”是Access中的保留字,因此如果要将其用作标识符,则需要将其转义。你已经知道如何做到这一点,因为你已经使用表名进行了操作,你的名字很差,里面有空格。