我正试图通过vb.net连接到Mysql数据库
但是我遇到了一个错误:

Unable to connect to any of the specified MySQL hosts

代码如下:
    Imports MySql.Data.MySqlClient
Public Class DBProject_1
    Dim MysqlConn As MySqlConnection

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    MysqlConn = New MySqlConnection
    MysqlConn.ConnectionString = "server=localhost;userid=root;password=root;database=My_Mysql_Database"
    Try
        MysqlConn.Open()
        MessageBox.Show("Connection Successful")
        MysqlConn.Close()
    Catch ex As Exception
        MessageBox.Show(ex.Message)
    Finally
        MysqlConn.Dispose()

    End Try
End Sub
End Class

我甚至试过用
MysqlConn.ConnectionString = "server=**127.0.0.1**;userid=root;password=root;database=My_Mysql_Database"

这可能是原因:
当我在Mysql工作台中测试Mysql数据库的连接时,测试失败。
错误是Failed to Connect to Mysql at 127.0.0.1:3306 with user root

最佳答案

试试这个ConnectionString

 MysqlConn.ConnectionString = "Server=localhost;Database=My_Mysql_Database;Uid=root;Pwd=root;"

关于mysql - 检查连接时无法连接到任何指定的MySQL主机,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/22962923/

10-14 13:56
查看更多