Vb.net 处置 Public Structure LoginData Public Sub New(loginType As String, Valid As Boolean) Type = loginType Success = Valid End Sub Public Type As String Public Success As Boolean End Structure Private Function ValidateLogin(User As String, PWord As String) As LoginData Dim table As New DataTable Using myConnection As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\User\Documents\RGIT\College\Semester 2 Assignments\Programming\College.accdb") 'stringPass = frmProgressBar.stringRecieve Using cmd As OleDbCommand = New OleDbCommand("Select Count(*), LoginType From Users Where Username = @Username and Password = @Password;", myConnection) cmd.Parameters.Add("@Username", OleDbType.VarChar, 50).Value = User cmd.Parameters.Add("@Password", OleDbType.VarChar, 50).Value = PWord myConnection.Open() table.Load(cmd.ExecuteReader) End Using End Using 'The AndAlso Stops evaluating as soon as it finds a False 'This prevents an error when we try to access Row 0 in the table when it doesn't exist If table.Rows.Count > 0 AndAlso CInt(table(0)(0)) = 1 Then Return New LoginData(table(0)(1).ToString, True) Else Return New LoginData(Nothing, False) End If End Function Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click 'Do the validation in the User Interface code 'The OrElse is a smidge quicker because it stops as soon as it finds True If txtUserName.Text = Nothing OrElse txtPassword.Text = Nothing Then MsgBox("Fill in all fields", MsgBoxStyle.Exclamation) Return End If Dim LD = ValidateLogin(txtUserName.Text, txtPassword.Text) If LD.Success Then MsgBox("Login Unsuccessful, unregistered username or incorrect password entered", MsgBoxStyle.Critical) txtPassword.Clear() txtUserName.Clear() txtUserName.Focus() Return End If stringPass = LD.Type Me.Visible = False frmProgressBar.Visible = True End Sub
请注意,用户界面代码(单击按钮)对数据库一无所知,而数据库代码对UI一无所知。这将允许您在项目中开始分层;与UI层分离的数据访问层 编辑Vb.net 处置 Public Structure LoginData Public Sub New(loginType As String, Valid As Boolean) Type = loginType Success = Valid End Sub Public Type As String Public Success As Boolean End Structure Private Function ValidateLogin(User As String, PWord As String) As LoginData Dim table As New DataTable Using myConnection As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\User\Documents\RGIT\College\Semester 2 Assignments\Programming\College.accdb") 'stringPass = frmProgressBar.stringRecieve Using cmd As OleDbCommand = New OleDbCommand("Select Count(*), LoginType From Users Where Username = @Username and Password = @Password;", myConnection) cmd.Parameters.Add("@Username", OleDbType.VarChar, 50).Value = User cmd.Parameters.Add("@Password", OleDbType.VarChar, 50).Value = PWord myConnection.Open() table.Load(cmd.ExecuteReader) End Using End Using 'The AndAlso Stops evaluating as soon as it finds a False 'This prevents an error when we try to access Row 0 in the table when it doesn't exist If table.Rows.Count > 0 AndAlso CInt(table(0)(0)) = 1 Then Return New LoginData(table(0)(1).ToString, True) Else Return New LoginData(Nothing, False) End If End Function Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click 'Do the validation in the User Interface code 'The OrElse is a smidge quicker because it stops as soon as it finds True If txtUserName.Text = Nothing OrElse txtPassword.Text = Nothing Then MsgBox("Fill in all fields", MsgBoxStyle.Exclamation) Return End If Dim LD = ValidateLogin(txtUserName.Text, txtPassword.Text) If LD.Success Then MsgBox("Login Unsuccessful, unregistered username or incorrect password entered", MsgBoxStyle.Critical) txtPassword.Clear() txtUserName.Clear() txtUserName.Focus() Return End If stringPass = LD.Type Me.Visible = False frmProgressBar.Visible = True End Sub,vb.net,ms-access,Vb.net,Ms Access,请注意,用户界面代码(单击按钮)对数据库一无所知,而数据库代码对UI一无所知。这将允许您在项目中开始分层;与UI层分离的数据访问层 编辑 忘了提到密码应该永远不能存储为纯文本。在学习登录之前,你应该学会对密码进行加密和散列。我将把这件事留给你和你的导师来纠正。嘿……非常感谢你的帮助。我确实尝试过将“Users”(数据库中的表名)添加到新的datatable中。示例…新数据表(“用户”)。还是给了我同样的错误。很抱歉,这是我第一年写代码。请原谅缺少正确的术语。VBA有一些调试区域(查看->立即/局
忘了提到密码应该永远不能存储为纯文本。在学习登录之前,你应该学会对密码进行加密和散列。我将把这件事留给你和你的导师来纠正。嘿……非常感谢你的帮助。我确实尝试过将“Users”(数据库中的表名)添加到新的datatable中。示例…新数据表(“用户”)。还是给了我同样的错误。很抱歉,这是我第一年写代码。请原谅缺少正确的术语。VBA有一些调试区域(查看->立即/局部/监视)。打开那些。注释掉你的代码,看看前几行是否有效。例如,
Dim cmd As OleDbCommand=New OleDbCommand(str,myConnection)
可能应该是两行。好的,我将尝试一下……再次感谢。将告诉您它是否有效问题是这一行:“如果(table.Rows.Count>0)和(txtUserName.Text)=(table.Rows(0)(2.ToString())和(txtPassword.Text=table.Rows(0)(3.ToString())”@VISQL,则OP正在使用VB.NET。记住这一点,很清楚什么是a,什么是a。嗨……谢谢你的帮助。我真的试过了。当我这样做时,它要么抛出相同的错误,要么按钮根本不起作用。你认为这是我写代码的方式吗?“结构、语法还是什么?”阿什顿我更新了我的答案,再往上一点插入一个新行检查。试试这个,很有魅力,兄弟。非常感谢你。非常感谢,非常感谢。我显然有很多东西要学。这非常有帮助。再次感谢
If (txtUserName.Text) = (table.Rows(0)(2).ToString()) And (txtPassword.Text = table.Rows(0)(3).ToString()) Then
table.Rows.Count > 0
If txtUserName.Text = Nothing Or txtPassword.Text = Nothing Then
MsgBox("Fill in all fields", MsgBoxStyle.Exclamation)
myConnection.Close()
ElseIf (table.Rows.Count < 1) Then
MsgBox("Login Unsuccessful, unregistered username or incorrect password entered", MsgBoxStyle.Critical)
txtPassword.Clear()
txtUserName.Clear()
txtUserName.Focus()
myConnection.Close()
table.DataSet.Clear()
Else
If (txtUserName.Text) = (table.Rows(0)(2).ToString()) And (txtPassword.Text = table.Rows(0)(3).ToString()) Then
If (table.Rows(0)(4).ToString()) = "Admin" Then
stringPass = "Admin"
Me.Visible = False
frmProgressBar.Visible = True
myConnection.Close()
ElseIf (table.Rows.Count > 0) And (table.Rows(0)(4).ToString()) = "Employee" Then
stringPass = "Employee"
Me.Visible = False
frmProgressBar.Visible = True
myConnection.Close()
Else
stringPass = "Student"
Me.Visible = False
frmProgressBar.Visible = True
myConnection.Close()
End If
Else
MsgBox("Login Unsuccessful, unregistered username or incorrect password entered", MsgBoxStyle.Critical)
txtPassword.Clear()
txtUserName.Clear()
txtUserName.Focus()
myConnection.Close()
table.DataSet.Clear()
End If
End If'''
Public Structure LoginData
Public Sub New(loginType As String, Valid As Boolean)
Type = loginType
Success = Valid
End Sub
Public Type As String
Public Success As Boolean
End Structure
Private Function ValidateLogin(User As String, PWord As String) As LoginData
Dim table As New DataTable
Using myConnection As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\User\Documents\RGIT\College\Semester 2 Assignments\Programming\College.accdb")
'stringPass = frmProgressBar.stringRecieve
Using cmd As OleDbCommand = New OleDbCommand("Select Count(*), LoginType From Users Where Username = @Username and Password = @Password;", myConnection)
cmd.Parameters.Add("@Username", OleDbType.VarChar, 50).Value = User
cmd.Parameters.Add("@Password", OleDbType.VarChar, 50).Value = PWord
myConnection.Open()
table.Load(cmd.ExecuteReader)
End Using
End Using
'The AndAlso Stops evaluating as soon as it finds a False
'This prevents an error when we try to access Row 0 in the table when it doesn't exist
If table.Rows.Count > 0 AndAlso CInt(table(0)(0)) = 1 Then
Return New LoginData(table(0)(1).ToString, True)
Else
Return New LoginData(Nothing, False)
End If
End Function
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
'Do the validation in the User Interface code
'The OrElse is a smidge quicker because it stops as soon as it finds True
If txtUserName.Text = Nothing OrElse txtPassword.Text = Nothing Then
MsgBox("Fill in all fields", MsgBoxStyle.Exclamation)
Return
End If
Dim LD = ValidateLogin(txtUserName.Text, txtPassword.Text)
If LD.Success Then
MsgBox("Login Unsuccessful, unregistered username or incorrect password entered", MsgBoxStyle.Critical)
txtPassword.Clear()
txtUserName.Clear()
txtUserName.Focus()
Return
End If
stringPass = LD.Type
Me.Visible = False
frmProgressBar.Visible = True
End Sub