使用 MySQL 数据库登录 VB.NET [英] VB.NET login with a MySQL database

查看:46
本文介绍了使用 MySQL 数据库登录 VB.NET的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我确实有一个烦人的问题,我无法解决这个问题.我的问题是我无法确认我的登录,某处是一个逻辑错误,因为我的 try-catch 块没有捕获"任何东西,我什至在数据库打开和 DB.Close 之间使用断点来查看是否有任何问题.以下是一些屏幕:

I do have an annoying problem here, I am not able troubleshoot this issue. My problem is that I cannot confirm my login, somewhere's a logical error because my try-catch block is not 'catching' anything, I even used breakpoints between DataBase Opening and DB.Close to see if there's any issue. Here are some screens :

所以如果我输入用户 Gigel 和他的密码 123(它是加密的),我会从我的 IF 中得到我的错误执行,那里出了点问题"

So if I enter the user Gigel and his password 123 (it's encrypted) I get my false execution from my IF , 'Something's wrong out there'

错误...,有人吗?

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

推荐答案

试试这个:

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

这篇关于使用 MySQL 数据库登录 VB.NET的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆