【发布时间】:2014-01-09 06:00:33
【问题描述】:
我的代码基于我的教授。我输入了正确的用户名和密码,但它一直说无效用户。表的名称也是正确的。我不知道为什么它一直说它是一个无效用户请帮助我。
If Len(Trim(txtUsername.Text)) = 0 Then
MessageBox.Show("Please enter user name", "Input Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
txtUsername.Focus()
Exit Sub
End If
If Len(Trim(txtPassword.Text)) = 0 Then
MessageBox.Show("Please enter password", "Input Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
txtPassword.Focus()
Exit Sub
End If
Dim query As String = "SELECT * FROM Users"
Dim dt As New DataTable
ExecuteQuery(query)
Dim ctr As Integer
If dt.Rows.Count > 0 Then
If (dt.Rows(ctr)("Username")) = txtUsername.Text Then
If (dt.Rows(ctr)("Password")) = txtPassword.Text Then
frmMainMenu.Show()
Me.Hide()
End If
Else
MsgBox("Incorrect password")
txtPassword.Text = ""
txtPassword.Focus()
End If
Else
MsgBox("Invalid user")
txtPassword.Text = ""
txtUsername.Text = ""
txtUsername.Focus()
End If
【问题讨论】:
-
那么您选择整个表,这将返回列表中的所有用户,然后您将用户名和密码与其中一个而不是所有用户进行比较。你的 SQL 应该被参数化,你应该执行类似
SELECT * FROM Users WHERE username=? and password=?