简体   繁体   English

VB.NET连接到MySQL服务器数据库

[英]VB.NET Connect to MySQL Server Database

I'm using the following code to connect to a MySQL database hosted with GoDaddy. 我正在使用以下代码连接到GoDaddy托管的MySQL数据库。 According to multiple references, this should work, but I get the error: 根据多个参考,这应该可以,但是出现错误:

MySql.Data.MySqlClient.MySqlException (0x80004005): Unable to connect to any of the specified MySQL hosts. MySql.Data.MySqlClient.MySqlException(0x80004005):无法连接到任何指定的MySQL主机。 ---> System.Net.Sockets.SocketException (0x80004005): This is usually a temporary error during hostname resolution and means that the local server did not receive a response from an authoritative server ---> System.Net.Sockets.SocketException(0x80004005):这通常是主机名解析过程中的临时错误,表示本地服务器未收到权威服务器的响应

Im wondering if I've configured something wrong as the words Local Server suggest that its not attempting to connect to the server at all? 我想知道我是否配置了某些错误,因为“ 本地服务器 ”一词表明它根本不尝试连接到服务器?

Code: 码:

Imports MySql.Data.MySqlClient
Module Licence_Check

    Dim conn As New MySqlConnection

    Public Sub ConnectToDatabase()

        Dim DatabaseName As String = "ALVEAREKEYSCHK"

        Dim server As String = "XXXX.db.XXXXXX.hostedXXXXX.com"

        Dim userName As String = "ALVEAREKEYSCHK"

        Dim password As String = "SXXXXXXXXXX!"

        If Not conn Is Nothing Then conn.Close()

        conn.ConnectionString = String.Format("server={0}; user id={1}; password={2}; database={3}; pooling=false", server, userName, password, DatabaseName)

        Try

            conn.Open()



            MsgBox("Connected")

        Catch ex As Exception

            Debug.Print(ex.ToString)
            MsgBox(ex.Message)

        End Try

        conn.Close()


    End Sub

End Module

Some things have been replaced with X's 有些东西已被X代替

UPDATE 1 更新1

In response to the answers, I've updated the code to: 为了回答这些问题,我将代码更新为:

    Dim conn As New MySqlConnection("server=SERVERNAMEHERE;uid=USERIDHERE;pwd=PASSWORDHERE;database=DATABASEHERE;")
    Try
        conn.Open()
        MsgBox("connection open")
    Catch ex As Exception
        Debug.Print(ex.Message)
    Finally
        conn.Close()
    End Try

End Sub

Something seems to have changed as now I'm getting the following error: 似乎有些变化,因为现在我收到以下错误:

' SERVERNAMEHERE ' for user ' USERIDISHERE ' using method 'mysql_native_password' failed with message: Access denied for user ' USERIDISHERE ' (using password: YES) 使用方法'mysql_native_password'为用户' USERIDISHERE '的' SERVERNAMEHERE '失败,并显示以下消息:用户' USERIDISHERE '的访问被拒绝(使用密码:是)

I'm guessing this is a server side issue now? 我猜这是服务器端的问题吗? Is there something else I need to do? 还有什么我需要做的吗?

Try to use this 尝试使用这个

Dim conn As New MySqlConnection("server=host;uid=db_id;pwd=db_password;database=db_name;")
Try
conn.open()
Msg("connection open")
catch ex as Exception
Msg(ex.message)
Finally
conn.close()
End Try

You can put the MySqlConnection credentials right away no need to declare it. 您可以立即放置MySqlConnection凭据,而无需对其进行声明。 i hope this help's. 我希望这有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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