2014-04-08 152 views
0

我確實有一個惱人的問題在這裏,我無法解決這個問題。我的問題是,我無法確認我的登錄,因爲我的try-catch塊沒有'捕捉'任何東西,我甚至在DataBase Opening和DB.Close之間使用了斷點來查看是否有任何問題。下面是一些屏幕:VB.NET登錄MySQL數據庫

所以,如果我進入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 
+0

'HashedPass'是空的,當你將它作爲一個參數,所以'Command.ExecuteScalar'將返回'0' – equisde

+0

我沒有改正它,但我有相同的邏輯錯誤。如果我評論散列部分並直接作爲參數使用TextBox2.text(我輸入密碼的文本框),它可以工作(輸入完整的加密代碼)。我的錯誤出現在哈希區域的某處,感謝輸入tho,哈希必須是= with something,lol – ExtremeSwat

+0

在'If'上放置一個斷點並檢查'Command.CommandText'的值。該值是將執行的SQL語句 – equisde

回答

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 
+0

感謝您的回答,它現在正在工作。 BIG UP – ExtremeSwat

+0

很高興幫助! :) – equisde