简体   繁体   English

mysql连接中的异常c#

[英]Exception in mysql connection c#

I got strange exception at connection.Open() to remote Mysql base. 我在connection.Open()到远程Mysql基地时遇到奇怪的异常。 This code is working fine but sometimes this error pops 此代码工作正常,但有时会弹出此错误

Here is a code: 这是一个代码:

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
                    {}
                }

Problem is when it comes at connection.Open(); 问题是什么时候连接.Open(); even if it's in try{}catch{} it's terminating application. 即使它在try {} catch {}它终止应用程序。

Exception log: 异常日志:

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

I don't known what to do. 我不知道该怎么做。 Tried everything and can't find because of what this is happening. 尝试了一切,因为发生了什么而无法找到。

System.Exception: Call to GetHostEntry failed after 00:00:00 while querying for hostname '' System.Exception:在查询主机名''时,在00:00:00之后调用GetHostEntry失败

Have a look at your calling code, see if there's some reason why Databaseserver is an empty string. 看看你的调用代码,看看为什么Databaseserver是一个空字符串的原因。 If that's not happening then wake your network boys up, as it would appear that your DNS is having having hiccups. 如果没有发生这种情况,请将您的网络男孩唤醒,因为看起来您的DNS出现了打嗝。 Don't know what your set up is but if you can afford to replace the names with IPs and it stops happening, then you can point a big dirty finger at them. 不知道你的设置是什么,但如果你能负担得起用IP替换名字并且它停止发生,那么你可以用一个大脏手指指着它们。

Could there be an Network and/or DNS issue: 可能存在网络和/或DNS问题:

System.Reflection.TargetInvocationException: An exception occurred during the operation, making the result invalid. System.Reflection.TargetInvocationException:操作期间发生异常,导致结果无效。 Check InnerException for exception details. 检查InnerException以获取异常详细信息。 InnerException. 的InnerException。 ---> MySql.Data.MySqlClient.MySqlException: Unable to connect to any of the specified MySQL hosts. ---> MySql.Data.MySqlClient.MySqlException: 无法连接到任何指定的MySQL主机。 ---> System.Exception: Call to GetHostEntry failed after 00:00:00 while querying for hostname '': SocketErrorCode=TryAgain, ErrorCode=11002, NativeErrorCode=11002. ---> System.Exception:在查询主机名''时,在00:00:00之后调用GetHostEntry失败: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.Sockets.SocketException: 这通常是主机名解析期间的临时错误,意味着本地服务器没有收到来自权威服务器的响应。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM