Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

vb.net - Unable to connect to any of the specified MySQL hosts when checking connection

I am trying to connect to Mysql database through vb.net However I run into the error:

Unable to connect to any of the specified MySQL hosts

enter image description here Here is the code:

    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

I have even tried to use

MysqlConn.ConnectionString = "server=**127.0.0.1**;userid=root;password=root;database=My_Mysql_Database"

This may be the reason: When I test connection of the Mysql database in Mysql Workbench the test fails. The error is Failed to Connect to Mysql at 127.0.0.1:3306 with user root enter image description here

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Try with this ConnectionString:

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...