使用VB.NET 2010连接到远程MySQL数据库

使用VB.NET 2010连接到远程MySQL数据库

问题描述:

我的连接字符串如下.

conn.ConnectionString = "Server=192.248.***.***; Port=3036; User id=admin; password=***; Database=abc; Connect Timeout=60;"

但是它返回错误连接数据库错误:无法连接到任何指定的MySQL主机."

But it returns error "Error connecting to database: Unable to connect to any of the specified MySQL hosts."

但是我可以使用PHPMyAdmin登录到同一网站.

But i can log in to the same using PHPMyAdmin.

//192.248.***.***/phpmyadmin

我还可以使用以下方式登录到本地MySQL数据库:

Also i can log in to local MySQL database using:

    conn.ConnectionString = "server=" & "localhost" & ";" & "user id=" & "admin" & ";" & "password=" & "" & ";" & "database=abc"

此代码有什么问题.我想连接到远程数据库,因为这是系统的要求.请帮忙.

What is the wrong in this code. I want to connect to the remote database since it is the requirement of the system. Any help please.

更新: 在立即窗口中显示:

Update: In immediate window it shows:

    PassbookPrinter.vshost.exe Error: 0 : Unable to connect to any of the specified MySQL hosts.
A first chance exception of type 'MySql.Data.MySqlClient.MySqlException' occurred in MySql.Data.dll

使用VB.Net连接到远程MySQL数据库

将vb.net连接到远程MySql数据库;无论您使用的是哪个VB.Net版本,都请执行以下步骤.

Connect to remote MySQL Database Using VB.Net

To connect vb.net to remote MySql database ; No matter what VB.Net version you might be using, just go throw the following steps.

1)从URL( https://dev.mysql .com/downloads/connector/net/)

2)安装连接器;默认情况下,连接器将安装在我已安装的版本的路径(C:\ Program Files \ MySQL \ Connector Net 6.9.6)中.

2) Install the connector; by default the connector will be installed in the path (C:\Program Files\MySQL\Connector Net 6.9.6) that's the version i have installed.

3)打开VB.Net IDE并启动新项目.

3) Open VB.Net IDE and start the new project.

4)将"Mysql.Data.dll"添加为对项目的引用,您可以在路径(C:\ Program Files \ MySQL \ Connector Net 6.9.6 \ Assemblies \ v4.5)中找到它. ;

4) Add the "Mysql.Data.dll" as a reference to your project, which you can find it in the path (C:\Program Files\MySQL\Connector Net 6.9.6\Assemblies\v4.5);

5)如下图所示准备您的连接表格;

5) Prepare your connection form as shown in this image;

6)创建名为数据库"的类,并编写以下代码.

6) Create the class named "Database" and write in the following code.

导入MySql.Data.MySqlClient

Imports MySql.Data.MySqlClient

公共班级数据库

Private _connection As New MySqlConnection
Private _errormessge As String
Private _servername As String
Private _databasename As String
Private _userid As String
Private _password As String

Public WriteOnly Property ServerName() As String
    Set(ByVal value As String)
        _servername = value
    End Set
End Property

Public WriteOnly Property DatabaseName() As String
    Set(ByVal value As String)
        _databasename = value
    End Set
End Property

Public WriteOnly Property UserID() As String
    Set(ByVal value As String)
        _userid = value
    End Set
End Property

Public WriteOnly Property Password() As String
    Set(ByVal value As String)
        _password = value
    End Set
End Property

Public ReadOnly Property ErrorMessage() As String
    Get
        Return _errormessge
    End Get
End Property

Public Function Connection() As Boolean
    Try
        _connection.ConnectionString = "Server=" & _servername & ";Port=3306;Database=" & _databasename & ";User ID=" & _userid & ";Password=" & _password & ""
        _connection.Open()
        If _connection.State = ConnectionState.Open Then
            _connection.Close()
            Return True
        End If
    Catch ex As Exception
        _errormessge = ex.Message
        Return False
    End Try
End Function

结束班级

公共类Frm_Main 私有Sub btn_connect_Click(ByVal发送者为System.Object,ByVal e为System.EventArgs)处理btn_connect.Click

Public Class Frm_Main Private Sub btn_connect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_connect.Click

'对象声明和实例化

将数据作为新数据库

    With data
        'Assing the object property values
        .ServerName = txt_server.Text
        .DatabaseName = txt_database.Text
        .UserID = txt_uid.Text
        .Password = txt_pwd.Text

        'Connection testing
        If .Connection Then
            MessageBox.Show("Database Conneted.")
        Else
            MessageBox.Show(.ErrorMessage)
        End If
    End With
End Sub

Private Sub btn_exit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_exit.Click
    Close()
End Sub

结束班级

7)运行项目并尝试连接;如果连接成功,那么您的运气好;如果连接失败,并显示以下错误消息,请放心继续阅读更多内容;

7) Run the project and try the connection; if the connection is successfully then your luck; and if the connection is not successfully with the following error message worry not just keep reading more;

8)注意@后的错误消息的IP地址(即您的IP),并将其添加到您的域cpanel远程mysql访问"中,下面的图像说明了远程mysql访问的样子(它们可能相同)延缓颜色);不要忘记按下添加主机"按钮.此设置可以每天为那些使用静态ip的用户使用.

8) Note the ip address on error message after @ (thats your ip) and add it to your domain cpanel "remote mysql access" the image bellow illustrates how the remote mysql access looks like(they are the same bu they may defer in colors); Don't forget to press "add hosts" button. This settings can work daily for those who are in static ip.

完成上述步骤后,查看成功消息;

See the message of success after the above steps;

但是,如果错误消息仍然存在,请尝试将密码文本保留为空白,如果远程数据库中没有密码,请再次连接;否则,请执行以下操作.如果错误再次出现,但将是"更改为否",则必须检查是否在DHCP中;

But if the error message persists try to leave the password text blank and connect again if you had no password in your remote database; if the error comes again except YES is changed to be NO then you have to check if your in DHCP;

9)如果您在DHCP中,则表示每个新的Internet连接中的IP都在变化.如果您使用的调制解调器可能在DHCP中.如果您使用的是动态ip,请检查ip的4个块中发生了什么变化.如果第一个连接中的第一个IP是197.250.3.201,下一个IP是197.250.60.70,下一个下一个IP是197.250.80.24;您必须在cpanel访问主机中添加197.250.%,才能使连接稳定.

9) If your in DHCP which means the ip is changing in every new Internet connection. If your using modem probably your in DHCP. If your in dynamic ips then check what is changing in the ip's 4 blocks. If the first ip was 197.250.3.201 in the first connection and the next ip is 197.250.60.70 and the next next ip is 197.250.80.24; you have to add 197.250.% in your cpanel access hosts for your connection to be stable.

10)注意:随着百分号(通配符)流到ip地址的左侧,安全防护门也就越打开.如果发生新错误,请与您的域提供商联系,域中可能存在其他一些安全问题.谢谢!

10) Note: As the percent symbol (wild card) flows to the left side of the ip address the more the door of security becomes open. On new error please contact your domain provider there might be some other security issues in the domain. Thanks!