VB.NET使用MySQL数据库登录

时间:2014-04-08 13:34:39

标签: mysql vb.net

我这里有一个烦人的问题,我无法解决这个问题。我的问题是我无法确认我的登录信息,因为我的try-catch模块没有“捕捉”错误。任何事情,我甚至在DataBase Opening和DB.Close之间使用断点来查看是否存在任何问题。以下是一些屏幕:

因此,如果我输入用户Gigel和他的密码123(它已经加密),我会从我的IF中得到我的错误执行,“那里有些错误”。'

错误......,有人吗?

PhpMyAdmin try-catch-block error here

Imports MySql.Data
Imports MySql.Data.MySqlClient
Imports System.Security.Cryptography




Public Class Form1

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click

        Me.Close()
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim MySQLConnection As New MySqlConnection("Server = localhost;Database = users; Uid=root; Pwd = password ")

        Dim HashedPass As String = ""

        'Converts the Password into bytes, computes the hash of those bytes, and then converts them into a Base64 string

        Using MD5hash As MD5 = MD5.Create()

            System.Convert.ToBase64String(MD5hash.ComputeHash(System.Text.Encoding.ASCII.GetBytes(TextBox2.Text)))

        End Using


        'Counter

        Dim SqlQuery As String = "SELECT COUNT(*) From users1 WHERE username = @Username AND password = @Password; "

        MySQLConnection.Open()

        Dim Command As New MySqlCommand(SqlQuery, MySQLConnection)

        'Sanitising parameters

        Command.Parameters.Add(New MySqlParameter("@Username", TextBox1.Text))
        Command.Parameters.Add(New MySqlParameter("@Password", HashedPass))


        'checker
        If Command.ExecuteScalar() = 1 Then
            MsgBox("Thanks for logging in")
            Me.Hide()
        Else
            MsgBox("Something's wrong down there")
        End If


        MySQLConnection.Close()
    End Sub
End Class

1 个答案:

答案 0 :(得分:1)

试试这个:

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

Try

    Dim MySQLConnection As New MySqlConnection("Server = localhost;Database = users; Uid=root; Pwd = password ")

    Dim SqlQuery As String = "SELECT COUNT(*) From users1 WHERE username = @Username AND password = MD5(@Password); "

    MySQLConnection.Open()

    Dim Command As New MySqlCommand(SqlQuery, MySQLConnection)

    Command.Parameters.Add(New MySqlParameter("@Username", TextBox1.Text))
    Command.Parameters.Add(New MySqlParameter("@Password", TextBox2.Text))

    If Command.ExecuteScalar() = 1 Then
        MsgBox("Thanks for logging in")            
    Else
        MsgBox("Invalid username or password")
    End If

    MySQLConnection.Close()

Catch ex as Exception

   MsgBox(ex.Message)

End Sub