用户名和密码在数据库中时未检测到

时间:2016-09-21 18:43:00

标签: mysql database vb.net

尝试使用mysql作为数据库在VB中执行登录系统但是当它们在数据库中时未检测到用户名和密码

我正在尝试使用VB作为数据库在mysql中执行LogIn系统,但是当它们位于数据库中时,它将作为用户名和密码检测不到。

我的第二个代码示例:我的注册帐户代码,我可以将其添加到我的数据库中,而不是从中读取并登录。
(注册代码和登录代码的形式不同)

登录不起作用的代码:

 Private Sub Button_login_Click(sender As Object, e As EventArgs) Handles Button_login.Click
    conn = New MySqlConnection
    conn.ConnectionString =
        "server=localhost;userid=root;password=root;database=librarydatabase"
    Dim reader As MySqlDataReader

    Try
        conn.Open()
        query = "select * from librarydatabase.users where Users= ' " & TextBox_Username.Text & " ' and Passwordhash=' " & TextBox_Password.Text & " ' "

        command = New MySqlCommand(query, conn)
        reader = command.ExecuteReader
        Dim count As Integer
        count = 0
        While reader.Read
            count = count + 1

        End While

        If count = 1 Then
            MessageBox.Show("You are logged in")

        ElseIf count > 1 Then
            MessageBox.Show("Username or password already exist")

        Else MessageBox.Show("Incorrect username or password")

        End If

        conn.Close()
    Catch ex As MySqlException
        MessageBox.Show(ex.Message)
    Finally
        conn.Dispose()

    End Try
End Sub

我的注册码可以正常工作并将记录添加到我的数据库中:

Private Sub Button_Register2_Click(sender As Object, e As EventArgs) Handles Button_Register2.Click
    conn = New MySqlConnection
    conn.ConnectionString =
        "server=localhost;userid=root;password=root;database=librarydatabase"
    Dim reader As MySqlDataReader

    Try
        conn.Open()
        query = "insert into librarydatabase.users (Users,Passwordhash) values ('" & TextBox_Username.Text & "','" & TextBox_Password.Text & "')"

        command = New MySqlCommand(query, conn)
        reader = command.ExecuteReader

        MessageBox.Show("you are registered")
        conn.Close()
    Catch ex As MySqlException
        MessageBox.Show(ex.Message)
    Finally
        conn.Dispose()

    End Try
End Sub

0 个答案:

没有答案