2016-01-22 40 views
0

我有一个程序从用户处获取信息并使用Phpmyadmin将其记录到数据库中,我们的代码完全相同,除了我的朋友他无法登录。MySQL登录适用于我,但不是我的朋友使用VB

代码是在这里:

我们两个数据库名称,表和列是完全一样的,他也因此它存储账户注册到数据库,但是当他试图用相同的信息登录,它说这是不成功的。

SignUpForm(这工作)

Public Class frmSignup 
Dim ServerString As String = "Server=localhost;User Id=root;Password=;Database=accountinfo" 
Dim SQLConnection As MySqlConnection = New MySqlConnection 

Private Sub Form3_Load(sender As Object, e As EventArgs) Handles MyBase.Load 
    SQLConnection.ConnectionString = ServerString 

    Try 
     If SQLConnection.State = ConnectionState.Closed Then 
      SQLConnection.Open() 
      MsgBox("Successfully connected to DB") 

     Else 
      SQLConnection.Close() 
      MsgBox("Failed to connect to DB") 
     End If 
    Catch ex As Exception 
     MsgBox(ex.ToString) 

    End Try 
End Sub 

Public Sub SaveAccountInformation(ByRef SQLStatement As String) 
    Dim cmd As MySqlCommand = New MySqlCommand 

    With cmd 
     .CommandText = SQLStatement 
     .CommandType = CommandType.Text 
     .Connection = SQLConnection 
     .ExecuteNonQuery() 
    End With 
    SQLConnection.Close() 
    SQLConnection.Dispose() 
End Sub 

Private Sub btnSignup_Click(sender As Object, e As EventArgs) Handles btnSignup.Click 
    If txtPasswd.Text = txtPasswd2.Text Then 
     MessageBox.Show("Passwords Match!") 

     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() 

      HashedPass = System.Convert.ToBase64String(MD5hash.ComputeHash(System.Text.Encoding.ASCII.GetBytes(txtPasswd.Text))) 

     End Using 


     Dim SQLStatement As String = "INSERT INTO accountinfodb(`Usernames`, `Passwords`) VALUES ('" & txtUsername.Text & "','" & HashedPass & "')" 
     SaveAccountInformation(SQLStatement) 



     MessageBox.Show("Account Successfully Registered") 
     frmLogin.Show() 
     frmLoginScreen.Hide() 
    Else 
     MessageBox.Show("Passwords Do Not Match!") 
     txtPasswd.Text = Focus() 
     txtPasswd.Clear() 
     txtPasswd2.Text = Focus() 
     txtPasswd2.Clear() 

    End If 
End Sub 
End Class 

登录表单(这不起作用寻找,但它为我工作)

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

Public Class frmLogin 

Private Sub btnLogin_Click(sender As Object, e As EventArgs) Handles btnLogin.Click 
    Dim conStr = "Server=localhost;User Id=root;Password=;Database=accountinfo" 
    Dim SQL = "SELECT * FROM accountinfodb WHERE Usernames = @uname AND `Passwords` = @pword" 

    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() 

     HashedPass = System.Convert.ToBase64String(MD5hash.ComputeHash(System.Text.Encoding.ASCII.GetBytes(txtPasswd.Text))) 

    End Using 

    ' this object will be closed and dispose @ End Using 
    Using dbCon As New MySqlConnection(conStr) 
     ' the command object likewise 
     Using cmd As New MySqlCommand(SQL, dbCon) 

      dbCon.Open() 
      cmd.Parameters.Add(New MySqlParameter("@uname", txtUsername.Text)) 
      cmd.Parameters.Add(New MySqlParameter("@pword", HashedPass)) 

      ' create a Using scope block for the reader 
      Using rdr As MySqlDataReader = cmd.ExecuteReader 

       If rdr.HasRows Then 
        MessageBox.Show("Welcome, " & txtUsername.Text) 
        frmProduct.Show() 
       Else 
        MessageBox.Show("Oops! Login unsuccessful!(Password/Username may be wrong, or the user may not exist!") 
        txtUsername.Clear() 
        txtUsername.Focus() 
        txtPasswd.Clear() 
       End If 
      End Using 
     End Using   ' close/dispose command 

    End Using    ' close/dispose connection 


End Sub 
End Class 

也想提

我分享我的文件在谷歌驱动器与他,所以他没有复制和粘贴任何代码。这是从我的电脑完全相同的文件。

回答

0

好的我发现这个问题,他使用的是过时的MySQL版本,而我的版本是最新的。我重新安装适当的MySQL服务器到最新版本,它的工作!

相关问题