简体   繁体   中英

Host x is not allowed to connect to this mysql server

I am trying to make a simple registration & activation system using my mysql server and vb.net and I am using the code below:

Imports MySql.Data.MySqlClient

Public Class ActivateMe
Dim MysqlConn As MySqlConnection
Dim myAdapter As New MySqlDataAdapter
Dim myData As MySqlDataReader

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    MysqlConn = New MySqlConnection()
    Try
        MysqlConn.Open()
        Dim checkUsername As String = "SELECT COUNT(*) FROM users WHERE verif=@p1 and username=@p2"
        Dim insertData As String = "INSERT INTO users(hasVerif) VALUES(@p3)"

        Using MysqlConn = New MySqlConnection(mysqlconntxt4reg)
            Using myCommand = New MySqlCommand(checkUsername, MysqlConn)
                MysqlConn.Open()
                myCommand.Parameters.AddWithValue("@p1", TextBox1.Text)
                myCommand.Parameters.AddWithValue("@p2", currentRegUser)
                Dim result = myCommand.ExecuteScalar()
                If result IsNot Nothing AndAlso Convert.ToInt32(result) > 0 Then
                    Using myCommand2 = New MySqlCommand(insertData, MysqlConn)
                        myCommand.Parameters.AddWithValue("@p3", 1)
                        myCommand2.ExecuteNonQuery()
                        MsgBox("Successfully Activated! You May Now Login!", MsgBoxStyle.Information, "Success")
                        Me.Close()
                    End Using
                Else
                    MsgBox("Invalid Activation Code", MsgBoxStyle.Critical, "Error")
                End If
            End Using
        End Using
        MysqlConn.Close()
    Catch myerror As MySqlException
        MessageBox.Show("Cannot connect to database: " & vbNewLine & vbNewLine & myerror.Message)
    Finally
        MysqlConn.Dispose()
    End Try

End Sub
End Class

The program sends the activation code to the email successfully and uploads the activation code to the database for checking but when I am on the activation form and enter the code, it says:

Host (my pc name) is not allowed to connect to this mysql server

In the different forms, the server accepts the connection and allows access to the database so I don't see why it would be different here... Please send me help! Thanks rodit

I'm not very familiar with MySQL under .NET but I believe you do something a bit odd here. First you open a connection without a connection string (I assume that uses default parameters)

MysqlConn = New MySqlConnection()
Try
    MysqlConn.Open()

and then you open another connection using an explicit string

Using MysqlConn = New MySqlConnection(mysqlconntxt4reg)
    Using myCommand = New MySqlCommand(checkUsername, MysqlConn)
        MysqlConn.Open()

Notice it's the same MysqlConn variable (with different objects).

So the MySqlException you're catching could be coming from any one of them. You can use that exception to find out which of the open calls actually triggered the exception (I assume it's first one).

You should probably also cleanup code and keep only one open call.

Andrei

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM