简体   繁体   中英

Exception in mysql connection c#

I got strange exception at connection.Open() to remote Mysql base. 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(); even if it's in try{}catch{} it's terminating application.

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 ''

Have a look at your calling code, see if there's some reason why Databaseserver is an empty string. If that's not happening then wake your network boys up, as it would appear that your DNS is having having hiccups. 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.

Could there be an Network and/or DNS issue:

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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