我正在研究MySql .NET连接器类的包装器。到目前为止,它已正常工作,没有缺陷。但是,我必须改变一些东西,因为它根本不会连接到我的MySql服务器,我可以通过phpMyAdmin访问服务器。
现在,我有以下代码:
Public Function Connect(ByVal Host As String,
ByVal Database As String,
ByVal Username As String,
ByVal Password As String) As Boolean
Try
_ConnInfo.Host = Host
_ConnInfo.Username = Username
_ConnInfo.Password = Password
_ConnInfo.Database = Database
_ConnInfo.ConnectionString = String.Format("server={0}; user id={1}; password={2}; database={3}; pooling=false",
_ConnInfo.Host,
_ConnInfo.Username,
_ConnInfo.Password,
_ConnInfo.Database)
_mysqlConn.ConnectionString = _ConnInfo.ConnectionString
_mysqlConn.Open()
CreateEventArgs(eArgs, currentID, False, "Open Connection", _ConnInfo, Me, MySQL_Wrapper_Operation.Connect, Nothing)
RaiseEvent OperationCompleted(eArgs)
OnConnect()
Return True
Catch ex As Exception
OnConnectError(ex)
CreateEventArgs(eArgs, currentID, True, "Open Connection", _ConnInfo, Me, MySQL_Wrapper_Operation.Connect, Nothing, ex)
RaiseEvent OperationCompleted(eArgs)
Return False
End Try
End Function
_ConnInfo
是用于存储MySql连接信息的类的实例:
Public Class MySQL_Wrapper_ConnectionInfo
Public Sub New()
End Sub
Public Sub New(ByVal _Host As String,
ByVal _Database As String,
ByVal _Username As String,
ByVal _Password As String,
Optional ByVal _ConnectionString As String = "")
Host = _Host
Database = _Database
Username = _Username
Password = _Password
ConnectionString = _ConnectionString
End Sub
Public Host As String
Public Database As String
Public Username As String
Public Password As String
Public ConnectionString As String
End Class
无论如何,当我调用Connect("10.131.120.46", "studenttest", "studenttest", "********")
时,我的函数返回false,我得到的错误是:
无法连接到任何指定的MySQL主机。
我尝试使用另一个连接字符串来自here:
_ConnInfo.ConnectionString = $"Server={_ConnInfo.Host};Database={_ConnInfo.Database};Uid={_ConnInfo.Username};Pwd={_ConnInfo.Password};Pooling=False;"
以上给出了相同的结果。在运行时,我能够找到连接字符串,即:
服务器= 10.131.120.46; user id = studenttest;密码= ********;数据库= studenttest;池=假
我只想指出错误发生在_mysqlConn.Open()
(其中_mysqlConn
在类中声明为Private Property _mysqlConn As New MySqlConnection
。
如果有人能在我犯错的地方给我一个头脑,我会非常感激。
提前致谢。