gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 01:43:19 25 4
gpt4 key购买 nike

我的连接字符串如下。

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

但它返回错误“连接到数据库时出错:无法连接到任何指定的 MySQL 主机。”

但我可以使用 PHPMyAdmin 登录。

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

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

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

这段代码有什么问题。我想连接到远程数据库,因为这是系统的要求。请提供任何帮助。

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

    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 版本,只需执行以下步骤即可。

1) 从 url ( https://dev.mysql.com/downloads/connector/net/ ) 下载 Mysql Connector/Net

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

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

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

5) 如图所示准备连接表; enter image description here

6) 创建名为“Database”的类并写入以下代码。

数据库类代码

导入 MySql.Data.MySqlClient

公共(public)类数据库

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

下课

表单类代码

公共(public)类 Frm_Main Private Sub btn_connect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) 处理 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) 运行项目并尝试连接;如果连接成功,那么你的运气;如果连接不成功并出现以下错误消息,请担心不要继续阅读更多内容; enter image description here

8) 注意@之后的错误消息中的IP地址(那是你的IP)并将其添加到你的域cpanel“远程mysql访问”下图说明了远程mysql访问的样子(它们是相同的,但它们可能延迟颜色);不要忘记按“添加主机”按钮。对于静态 IP 的用户,此设置每天都可以使用。 enter image description here

完成上述步骤后看到成功信息;

enter image description here

但是如果错误消息仍然存在,如果您的远程数据库中没有密码,请尝试将密码文本留空并重新连接;如果错误再次出现,除了 YES 被更改为 NO 那么你必须检查你是否在 DHCP;

9) 如果您在 DHCP 中,这意味着 IP 在每个新的 Internet 连接中都会发生变化。如果您使用的调制解调器可能是您的 DHCP。如果你在动态 ips 中,那么检查 ip 的 4 个 block 中发生了什么变化。如果第一次连接第一个ip是197.250.3.201,下一个ip是197.250.60.70,下一个ip是197.250.80.24;您必须在您的 cpanel 访问主机中添加 197.250.% 才能使连接稳定。 enter image description here

10) 注意:随着百分号(通配符)流向 ip 地址的左侧,安全之门打开的越多。如果出现新错误,请联系您的域提供商,域中可能存在其他一些安全问题。谢谢!

关于mysql - 使用 VB.NET 2010 连接到远程 MySQL 数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13797946/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com