Vb.net 使用Compare使用MS Access登录
我这里有一个小问题,没有错误,但它不工作,我有一个登录,它连接到MS ACCESS,MS ACCESS包含用户和密码,下面是包含问题的代码部分:Vb.net 使用Compare使用MS Access登录,vb.net,ms-access,Vb.net,Ms Access,我这里有一个小问题,没有错误,但它不工作,我有一个登录,它连接到MS ACCESS,MS ACCESS包含用户和密码,下面是包含问题的代码部分: 'Get Password from database Dim dbpassword As String = dt.Rows(0).Item("Password") 'compare password from database to the input password If
'Get Password from database
Dim dbpassword As String = dt.Rows(0).Item("Password")
'compare password from database to the input password
If password.CompareTo("dbpassword") = 0 Then
MsgBox("Successful!, Correct Password", MsgBoxStyle.OkOnly, "Correct")
Me.Close()
Form2.Show()
Else
MsgBox("Wrong Password!, Invalid Password", MsgBoxStyle.OkOnly, "Invalid")
End If
'If wrong username
Catch ex As Exception
MsgBox("Invalid Input!, Invalid Entry", MsgBoxStyle.OkOnly, "Invalid")
Me.TextBox1.Text = ""
Me.TextBox2.Text = ""
End Try
End If
End Sub
问题是它直接通过Catch ex AS Exception,即使用户和密码输入正确,Msgbox“Invalid Input”也会提示。
If password.CompareTo不起作用。这是我的全部代码:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'Declare Variables
Dim connection As New OleDbConnection
Dim username As String
Dim password As String
'Get username and password input
username = Me.TextBox1.Text.ToString
password = Me.TextBox2.Text.ToString
'Check username if NULL
If username = "" Then
MsgBox("Please Enter Username!, Invalid UserName", MsgBoxStyle.OkOnly, "Invalid")
Me.TextBox2.Text = ""
'Check password if NULL
ElseIf password = "" Then
MsgBox("Please Enter Password!, Invalid Password", MsgBoxStyle.OkOnly, "Invalid")
Me.TextBox1.Text = ""
Else
Try
'Establish database connection
connection = New OleDb.OleDbConnection
connection.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;; Data Source=" & Application.StartupPath & "\Data.mdb"
connection.Open()
'Input Query
Dim da As New OleDb.OleDbDataAdapter("Select * FROM tblUser WHERE User =" & username, connection)
Dim dt As New DataTable
'Fill data table
da.Fill(dt)
'Get Password from database
Dim dbpassword As String = dt.Rows(0).Item("Password")
'compare password from database to the input password
If password.CompareTo("dbpassword") = 0 Then
MsgBox("Successful!, Correct Password", MsgBoxStyle.OkOnly, "Correct")
Me.Close()
Form2.Show()
Else
MsgBox("Wrong Password!, Invalid Password", MsgBoxStyle.OkOnly, "Invalid")
End If
'If wrong username
Catch ex As Exception
MsgBox("Invalid Input!, Invalid Entry", MsgBoxStyle.OkOnly, "Invalid")
Me.TextBox1.Text = ""
Me.TextBox2.Text = ""
End Try
End If
End Sub
提前谢谢。试试换衣服
password.CompareTo("dbpassword")
password.CompareTo(dbpassword)
至
首先检查dt是否有值,并检查它是否等于值是否将捕获码更新为
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.OkOnly, "Invalid")
因此,我们可以得到实际的异常,然后发布异常如果您将字符串与文本“dbpassword”进行比较,该文本包含在双引号中,而不是变量dbpassword的值,请删除双引号quotation@Jade仍然会遇到相同的问题,先生。请尝试在此处添加断点[Dim dbpassword As String=dt.Rows(0.Item)(“Password”)]按F9并参见停止,如果按F8。这样,在跳转到捕获区域之前,您将逐行看到执行。仍然会遇到相同的问题sirTry password=dbpassword INSTEAD没有任何错误,问题是,程序忽略该条件,然后通过捕获框“Invalid input,Invalid entry”行。请不要简单地发布代码。给出一些关于代码的解释、信息或用法。例如,请参阅。@senthilkumar仍然得到相同的问题,没有任何更改。dt.Rows(0)。Item(“密码”)更改为dt.Rows(0)(“密码”).ToString()
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.OkOnly, "Invalid")