vb.net 区分大小写的用户名和密码
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13453764/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Case sensitive username and password
提问by Rara Arar
Im using this code:
我使用此代码:
Dim con As OleDbConnection = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source= UserPass.mdb;")
con.Open()
Dim str As String
str = "SELECT * FROM UserPass WHERE Username='" & txtUsername.Text & "' AND Password='" & txtPassword.Text & "'"
Dim cmd As OleDbCommand = New OleDbCommand(str, con)
cmd.Parameters.AddWithValue("user", txtUsername.Text)
cmd.Parameters.AddWithValue("pass", txtPassword.Text)
Dim sdr As OleDbDataReader = cmd.ExecuteReader()
' It will be case sensitive if you compare usernames here.
If sdr.HasRows Then
If sdr.Read Then
If txtPassword.Text <> sdr("Password").ToString And txtUsername.Text <> sdr("Username").ToString Then
MessageBox.Show(" Incorrect Username/Password. Login Denied ", " Error! ", MessageBoxButtons.OK, MessageBoxIcon.Error)
Else
MessageBox.Show(" You are now Logged In! ", " Welcome! ", MessageBoxButtons.OK, MessageBoxIcon.Asterisk)
frmOne.Show()
Me.Hide()
End If
End If
End If
sdr.Close()
con.Close()
This is the line where it should check the case of the letters, but it does not seem to work:
这是应该检查字母大小写的行,但它似乎不起作用:
If txtPassword.Text <> sdr("Password").ToString And txtUsername.Text <> sdr("Username").ToString Then
回答by Rob Gevers
Unless I am misunderstanding the And in the if statement, the logic you are using will only reject the login if both the username and password do not match. If the password does not match, but the user does, it will drop to the else block and log the user in.
除非我误解了 if 语句中的 And,否则您使用的逻辑只会在用户名和密码不匹配的情况下拒绝登录。如果密码不匹配,但用户匹配,它将进入 else 块并让用户登录。
Try changing it to:
尝试将其更改为:
If txtPassword.Text <> sdr("Password").ToString Or txtUsername.Text <> sdr("Username").ToString Then

