我有我的代碼從我的教授。我輸入了正確的用戶名和密碼,但它一直說無效的用戶。表格的名稱也是正確的。我不知道爲什麼它一直說這是一個無效的用戶請好心幫助我。我不知道它有什麼問題,你可以分析,如果可能
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 =?和密碼=?' – Namphibian