我的问题是我在我的程序中使用数据库中的数据时遇到了困难。我的意思是如何获取数据库中的用户名并将其用作VB中的变量?这是我的代码:
Imports System.Data.OleDb
Public Class LogIn
Dim provider As String
Dim dataFile As String
Dim connString As String
Dim myConnection As OleDbConnection = New OleDbConnection
'Default User Name and Password is set to "admin".
Public userName As String = "admin"
Public userPassword As String = "admin"
Public btnClick As Integer = 0
Private Sub LogIn_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
txtPassword.PasswordChar = "*"
End Sub
Private Sub btnLogIn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLogIn.Click
provider = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source ="
'Change the following to your access database location
dataFile = "C:\Users\Show Man\Desktop\LogIn1.accdb"
connString = provider & dataFile
myConnection.ConnectionString = connString
'the query:
Dim cmd As OleDbCommand = New OleDbCommand("SELECT * FROM [Users] WHERE [UserName] = '" & txtUserName.Text & "' AND [Password] = '" & txtPassword.Text & "'", myConnection)
' the following variable is hold true if user is found, and false if user is not found
Dim userFound As Boolean = False
myConnection.Open()
'if found:
Dim dr As OleDbDataReader = cmd.ExecuteReader
While dr.Read
userFound = True
End While
'Make Password not visible to the user. It only shows "*" and no text
'If no User Name and Password entered, access is not granted
txtPassword.PasswordChar = "*"
If txtPassword.Text = "" Or txtUserName.Text = "" Then Exit Sub
'If correct User Name and Password enterd, access is granted and opens the Student Form
If txtUserName.Text = userName And txtPassword.Text = userPassword Then
Me.Hide()
btnClick = 0
StudentDatabase.Show()
Else
'If wrong User Name or Password enterd a warning message box opens
MsgBox("Invalid Username or Password", MsgBoxStyle.Exclamation, "Log In")
txtPassword.Focus()
btnClick += 1
End If
If btnClick = 3 Then
MsgBox("3 Unsucessful attempts! Please Try Again Later!", vbCritical, "Locked Out")
Me.Close()
End If
End Sub
如何在数据库中使用UserName来测试密码是否正确,然后授予访问权限。
答案 0 :(得分:-1)
Imports System.Data.OleDb
Public Class LogIn
Dim connString As String
Dim myConnection As OleDbConnection = New OleDbConnection
Dim cmd As New OleDbCommand
Dim dr As OleDbDataReader
'Default User Name and Password is set to "admin".
Public userName As String = "admin"
Public userPassword As String = "admin"
Public btnClick As Integer = 0
Private Sub LogIn_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
txtPassword.PasswordChar = "*"
End Sub
Private Sub btnLogIn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLogIn.Click
'provider = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source ="
'Change the following to your access database location
'dataFile = "C:\Users\Show Man\Desktop\LogIn1.accdb"
'Instead of doing that (above), you can copy and paste the database file in the folder the projects form are located.
connString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & IO.Path.Combine(My.Application.Info.DirectoryPath, "dbSample.accdb") & ";Persist Security Info=False;Jet OLEDB:Database Password=" & userPassword & ""
'This password protects the database (the .accdb file is an MSAccess file, right? You can set a password to it so that not anyone can open it.)
myConnection.ConnectionString = connString
'the query:
myConnection.Open()
Dim cmd As OleDbCommand = New OleDbCommand("SELECT * FROM [Users] WHERE [UserName] = '" & txtUserName.Text & "' AND [Password] = '" & txtPassword.Text & "'", myConnection)
dr = cmd.ExecuteReader
If dr.Read Then
'Login Success!
btnCLick = 0
Me.Hide()
DirectForm.Show()
Elseif Not dr.Read Then
MsgBox("Invalid Username or Password", MsgBoxStyle.Exclamation, "Log In")
txtPassword.Focus()
btnClick += 1
If btnClick = 3 Then
MsgBox("3 Unsucessful attempts! Please Try Again Later!", vbCritical, "Locked Out")
Me.Close()
End If
End If
myConnection.Close()
End Sub