我在 connection.Open() 到远程 Mysql 库时遇到了奇怪的异常。此代码工作正常,但有时会弹出此错误
这是一个代码:
private static string
DatabaseServer = "",
DatabasePort = "",
DatabaseName = "",
DatabaseUserID = "",
DatabasePassword = "";
public static string ConnectionString = String.Format("SERVER={0};PORT={1};DATABASE={2};UID={3};PASSWORD={4}",
DatabaseServer, DatabasePort, DatabaseName, DatabaseUserID, DatabasePassword);
using (MySqlConnection connection = new MySqlConnection(ConnectionString))
{
try
{
if (connection != null)
{
connection.Open();
string query = @"....";
MySqlCommand cmd = new MySqlCommand(query, connection);
cmd.CommandType = CommandType.Text;
using (MySqlDataReader dataReader = cmd.ExecuteReader())
{
while (dataReader.Read())
{
}
}
}
}
catch
{}
}
问题是在connection.Open(); 即使它在 try{}catch{} 中,它也会终止应用程序。
异常日志:
System.Reflection.TargetInvocationException: An exception occurred during the operation, making the result invalid. Check InnerException for exception details. InnerException. ---> MySql.Data.MySqlClient.MySqlException: Unable to connect to any of the specified MySQL hosts. ---> System.Exception: Call to GetHostEntry failed after 00:00:00 while querying for hostname '': SocketErrorCode=TryAgain, ErrorCode=11002, NativeErrorCode=11002. ---> System.Net.Sockets.SocketException: 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.Dns.GetAddrInfo(String name)
в System.Net.Dns.InternalGetHostByName(String hostName, Boolean includeIPv6)
в System.Net.Dns.GetHostEntry(String hostNameOrAddress)
в MySql.Data.Common.StreamCreator.GetDnsHostEntry(String hostname)
--- ---
в MySql.Data.Common.StreamCreator.GetDnsHostEntry(String hostname)
в MySql.Data.Common.StreamCreator.GetHostEntry(String hostname)
в MySql.Data.Common.StreamCreator.GetStreamFromHost(String pipeName, String hostName, UInt32 timeout)
в MySql.Data.Common.StreamCreator.GetStream(UInt32 timeout)
в MySql.Data.MySqlClient.NativeDriver.Open()
--- ---
в MySql.Data.MySqlClient.NativeDriver.Open()
в MySql.Data.MySqlClient.Driver.Open()
в MySql.Data.MySqlClient.Driver.Create(MySqlConnectionStringBuilder settings)
в MySql.Data.MySqlClient.MySqlPool.GetPooledConnection()
в MySql.Data.MySqlClient.MySqlPool.TryToGetDriver()
в MySql.Data.MySqlClient.MySqlPool.GetConnection()
в MySql.Data.MySqlClient.MySqlConnection.Open()
в System.ComponentModel.BackgroundWorker.OnDoWork(DoWorkEventArgs e)
в System.ComponentModel.BackgroundWorker.WorkerThreadStart(Object argument)
--- ---
в System.ComponentModel.AsyncCompletedEventArgs.RaiseExceptionIfNecessary()
в System.ComponentModel.RunWorkerCompletedEventArgs.get_Result()
в Server.Engines.DonateSQL.bw_RunWorkerCompleted(Object sender, RunWorkerCompletedEventArgs e) в System.ComponentModel.BackgroundWorker.OnRunWorkerCompleted(RunWorkerCompletedEventArgs e)
в System.ComponentModel.BackgroundWorker.AsyncOperationCompleted(Object arg)
в System.Threading.QueueUserWorkItemCallback.WaitCallback_Context(Object state)
в System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
в System.Threading.QueueUserWorkItemCallback.System.Threading.IThreadPoolWorkItem.ExecuteWorkItem()
в System.Threading.ThreadPoolWorkQueue.Dispatch()
в System.Threading._ThreadPoolWaitCallback.PerformWaitCallback()
我不知道该怎么办。尝试了一切,但由于正在发生的事情而找不到。