1

我在 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()

我不知道该怎么办。尝试了一切,但由于正在发生的事情而找不到。

4

2 回答 2

2

System.Exception:在 00:00:00 后查询主机名“”时调用 GetHostEntry 失败

看看你的调用代码,看看 Databaseserver 是一个空字符串是否有某种原因。如果这没有发生,请唤醒您的网络男孩,因为您的 DNS 似乎出现了问题。不知道你的设置是什么,但如果你能负担得起用 IP 替换名称并且它停止发生,那么你可以用脏手指指着它们。

于 2013-03-18T23:56:02.540 回答
0

是否存在网络和/或 DNS 问题:

System.Reflection.TargetInvocationException:操作过程中发生异常,导致结果无效。检查 InnerException 以获取异常详细信息。内部异常。---> MySql.Data.MySqlClient.MySqlException:无法连接到任何指定的 MySQL 主机。---> System.Exception:在 00:00:00 后查询主机名“”时调用 GetHostEntry 失败:SocketErrorCode=TryAgain,ErrorCode=11002,NativeErrorCode=11002。---> System.Net.Sockets.SocketException:这通常是主机名解析期间的临时错误,意味着本地服务器没有收到来自权威服务器的响应。

于 2013-03-18T23:52:19.277 回答