且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

VB中的登录系统问题

更新时间:2023-12-04 20:13:58

您好b $ b

首先您需要移动
Hi
First you need to move the
Return

循环语句,因为在检查第一行时条件检查可能会失败。但是第二行中可能存在正确的用户名和密码组合。使用标志来设置要返回的值,并在循环结束后检查该标志。



以下是示例更改的代码:



statements out of the loop as condition check may fail when checking even the first row. But the correct user name and password combination may be present in the second row. Use flags to set the value to be returned and check that flag once the loop is over.

Here is the sample changed code:

Public Function Login(ByVal username As String, ByVal password As String)
        Dim usersDatasSet As New DataSet()
        usersDataAdapter.FillSchema(usersDatasSet, SchemaType.Source, "Users")
        usersDataAdapter.Fill(usersDatasSet, "Users")
        Dim table As DataTable = usersDatasSet.Tables("Users")

        Dim currentUser As String = String.Empty
        Dim currentPassword As String = String.Empty
        Dim IsValid As Boolean = False

        For i As Integer = 0 To table.Rows.Count - 1
            currentUser = table.Rows(i)("Username").ToString().Trim()
            currentPassword = table.Rows(i)("Password").ToString().Trim()

            'Check input
            If (currentUser <> username And currentPassword = password) Then
                IsValid = False
                ' MessageBox.Show("Unknown user", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
                'Return False

            ElseIf (currentUser = username And currentPassword <> password) Then
                IsValid = False
                'MessageBox.Show("Wrong password", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
                'Return False
            ElseIf (currentUser <> username AndAlso currentPassword <> password) Then
                IsValid = False
                'MessageBox.Show("Username and  password unknown", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
                'Return False
            ElseIf (currentUser = username AndAlso currentPassword = password) Then
                IsValid = True
                usersDatasSet.Dispose()
                Connection.Close()
                'Return True
            End If
        Next

        ' Check if Validation succeeded
        If IsValid = False Then
            'Show Message
            Return False
        Else
            Return True
        End If
        usersDatasSet.Dispose()
        Connection.Close()
        Return False
    End Function


谢谢你的回复。我会尝试这个
Thanks for your reply. I will try this