Пример #1
0
 private void HandleException(MySqlException ex)
 {
     if (ex.IsFatal)
     {
         owner.Close();
     }
 }
Пример #2
0
 private DbException HandleUniqueException(string message, MySqlException dataException)
 {
     var entry = string.Empty;
     var key = string.Empty;
     var findEntry = Regex.Match(dataException.Message, "'.*?'");
     if (findEntry.Success)
     {
         entry = findEntry.Value;
         key = dataException.Message.Replace("Duplicate entry {0} for key".Fi(entry), "").Trim();
     }
     return new DbException(message, dataException)
     {
         IsDuplicateKeyViolation = true,
         ErrorNumber = dataException.Number,
         KeyName = key.Replace("'", ""),
         KeyValue = entry.Replace("'", "")
     };
 }
Пример #3
0
        protected virtual void Dispose(bool disposing)
        {
            if (disposed)
            {
                return;
            }

            // Avoid cyclic calls to Dispose.
            disposed = true;
            try
            {
                ResetTimeout(1000);
                handler.Close(IsOpen);
                // if we are pooling, then release ourselves
                if (ConnectionString.Pooling)
                {
                    MySqlPoolManager.RemoveConnection(this);
                }
            }
            catch (Exception ex)
            {
                if (disposing)
                {
                    MySqlException mysqlEx = ex as MySqlException;
                    if (mysqlEx == null)
                    {
                        MySqlTrace.LogError(0, ex.GetBaseException().Message);
                    }
                    else
                    {
                        MySqlTrace.LogError(mysqlEx.Number, ex.GetBaseException().Message);
                    }
                }
            }
            finally
            {
                reader = null;
                IsOpen = false;
            }
        }
Пример #4
0
 protected virtual void Dispose(bool disposing)
 {
     if (this.disposed)
     {
         return;
     }
     this.disposed = true;
     try
     {
         this.ResetTimeout(1000);
         this.handler.Close(this.isOpen);
         if (this.connectionString.Pooling)
         {
             MySqlPoolManager.RemoveConnection(this);
         }
     }
     catch (Exception ex2)
     {
         if (disposing)
         {
             MySqlException ex = ex2 as MySqlException;
             if (ex == null)
             {
                 MySqlTrace.LogError(0, ex2.GetBaseException().Message);
             }
             else
             {
                 MySqlTrace.LogError(ex.Number, ex2.GetBaseException().Message);
             }
         }
     }
     finally
     {
         this.reader = null;
         this.isOpen = false;
     }
 }
Пример #5
0
        private void PrintMySQLDBException(MySqlException ex)
        {
            //When handling errors, you can your application's response based
            //on the error number.
            //The two most common error numbers when connecting are as follows:
            //0: Cannot connect to server.
            //1045: Invalid user name and/or password.

            switch (ex.Number)
            {
                case 0:
                    if (bMessageBoxShow)
                    {
                        MessageBox.Show("Cannot connect to server.  Contact administrator");
                    }
                    bMessageBoxShow = false;
                    break;

                case 1040:
                    if (bMessageBoxShow)
                    {
                        MessageBox.Show("Too many connections to DB");
                    }
                    bMessageBoxShow = false;
                    break;

                case 1045:
                    if (bMessageBoxShow)
                    {
                     MessageBox.Show("Invalid username/password, please try again");
                     }
                    bMessageBoxShow = false;
                    break;

                default:
                    mySave.InsertRow((int)Definition.Message.D_ALARM, " MySQL-Exception number:" + ex.Number + "\r\n" + ex.ToString());
                    Console.WriteLine(" MySQL-Exception number:" + ex.Number + "\r\n" + ex.ToString());
                    break;
            }
        }
Пример #6
0
        private void Crash(MySqlException m, bool logerror, bool c = false)
        {
            if(c)
            {
                _crash = true;
                Log.Error("MySql", sLConsole.GetString("Query error: {0}"), m.Message);
                Log.Warning("MySql", sLConsole.GetString("Program shutting down!"));
                SchumixBase.Quit(false);

                foreach(var nw in INetwork.WriterList)
                {
                    if(!nw.Value.IsNull())
                        nw.Value.WriteLine("QUIT :Sql connection crash.");
                }

                Thread.Sleep(1000);
                sRuntime.Exit();
            }

            if(m.Message.Contains("Fatal error encountered during command execution."))
            {
                _crash = true;
                Log.Error("MySql", sLConsole.GetString("Query error: {0}"), m.Message);
                Log.Warning("MySql", sLConsole.GetString("Program shutting down!"));
                SchumixBase.Quit(false);

                foreach(var nw in INetwork.WriterList)
                {
                    if(!nw.Value.IsNull())
                        nw.Value.WriteLine("QUIT :Sql connection crash.");
                }

                Thread.Sleep(1000);
                sRuntime.Exit();
            }

            if(m.Message.Contains("Timeout expired."))
            {
                _crash = true;
                Log.Error("MySql", sLConsole.GetString("Query error: {0}"), m.Message);
                Log.Warning("MySql", sLConsole.GetString("Program shutting down!"));
                SchumixBase.Quit(false);

                foreach(var nw in INetwork.WriterList)
                {
                    if(!nw.Value.IsNull())
                        nw.Value.WriteLine("QUIT :Sql connection timeout.");
                }

                Thread.Sleep(1000);
                sRuntime.Exit();
            }

            if(m.Message.Contains("Unable to connect to any of the specified MySQL hosts."))
            {
                _crash = true;
                Log.Error("MySql", sLConsole.GetString("Query error: {0}"), m.Message);
                Log.Warning("MySql", sLConsole.GetString("Program shutting down!"));
                SchumixBase.Quit(false);

                foreach(var nw in INetwork.WriterList)
                {
                    if(!nw.Value.IsNull())
                        nw.Value.WriteLine("QUIT :Sql connection timeout.");
                }

                Thread.Sleep(1000);
                sRuntime.Exit();
            }

            if(logerror)
                Log.Error("MySql", sLConsole.GetString("Query error: {0}"), m.Message);
        }
 private void HandleException(MySqlException ex)
 {
   if (ex.IsFatal)
     owner.Close();
 }
Пример #8
0
 public bool IsConnectionOkay()
 {
     var okay = true;
     try
     {
         MySqlConnection conn = new MySqlConnection(MySqlConnectionString);
         conn.Open();
     }
     catch (MySqlException ex)
     {
         okay = false;
         this.MySqlException = ex;
     }
     return okay;
 }
Пример #9
0
 public bool testConfigConnectionString(ref MySqlException returnEx)
 {
     try
     {
         conn.ConnectionString = configFileConnectionString;
         conn.Open();
         conn.Close();
         return true;
     }
     catch (MySql.Data.MySqlClient.MySqlException ex)
     {
         returnEx = ex;
         return false;
     }
 }
Пример #10
0
        //
        // WriteToEventLog
        //   A helper function that writes exception detail to the event log. Exceptions
        // are written to the event log as a security measure to avoid private database
        // details from being returned to the browser. If a method does not return a status
        // or boolean indicating the action succeeded or failed, a generic exception is also
        // thrown by the caller.
        //
        private void WriteToEventLog(MySqlException e, string action)
        {
            EventLog log = new EventLog();
            log.Source = eventSource;
            log.Log = eventLog;

            string message = exceptionMessage + "\n\n";
            message += "Action: " + action + "\n\n";
            message += "Exception: " + e.ToString();

            log.WriteEntry(message);
        }
Пример #11
0
 private void Crash(MySqlException m, bool logerror)
 {
     if(logerror)
         throw new CDatabaseException(string.Format(sLConsole.MySql("Text2"), m.Message));
 }
Пример #12
0
 void connectError(MySql.Data.MySqlClient.MySqlException ex)
 {
     MessageBox.Show(ex.Message);
 }
Пример #13
0
 private void WriteToEventLog(MySqlException e, string action)
 {
     EventLog log = new EventLog();
     log.Source = eventSource;
     log.Log = eventLog;
     string message = exceptionMessage + Environment.NewLine + Environment.NewLine;
     message += "Action: " + action + Environment.NewLine + Environment.NewLine;
     message += "Exception: " + e;
     log.WriteEntry(message);
 }
Пример #14
0
        private static void HandleException(MySqlException mySqlException)
        {
            MySqlErrorCode errorCode;
            if (!Enum.TryParse(mySqlException.Number.ToString(), false, out errorCode))
            {
                Debug.WriteLine("Unable to parse exception: " + mySqlException.Message);
            }

            if (errorCode == MySqlErrorCode.DatabaseAccessDenied)
            {
                throw new DatabaseAccessDeniedExcpetion("Error-Code: " + errorCode);
            }
            if (errorCode == MySqlErrorCode.UnableToConnectToHost)
            {
                MessageBox.Show("Unable to reach server");
                return;
            }

            Debug.WriteLine("MySqlException: " + errorCode + "\tMessage: " + mySqlException.Message + "\tInner-Exception " + mySqlException.InnerException?.Message);
        }
Пример #15
0
        public bool executeNonQueryCommand(string sqlCommand, ref MySqlException returnEx)
        {
            bool retVal = true;
            //myConnectionString = Properties.Settings.Default.connectionString;
            int temp;

            try
            {
                myTransCommand.CommandText = sqlCommand;
                if (myTransCommand.Connection.State.ToString() != "Open")
                    myTransCommand.Connection.Open();

                temp = myTransCommand.ExecuteNonQuery();

                retVal = true;
            }
            catch (MySql.Data.MySqlClient.MySqlException ex)
            {
                retVal = false;
                returnEx = ex;
            }

            return retVal;
        }
Пример #16
0
        public static tgConcurrencyException CheckForConcurrencyException(MySqlException ex)
        {
            tgConcurrencyException ce = null;

            if (ex != null)
            {
                if (ex.Number == 532)
                {
                    ce = new tgConcurrencyException(ex.Message, ex);
                    ce.Source = ex.Source;
                }
            }

            return ce;
        }
Пример #17
0
 public void rollBack(ref MySqlException returnEx)
 {
     try 
     { 
         myTrans.Rollback();
     }
     catch (MySql.Data.MySqlClient.MySqlException ex)
     {
         returnEx = ex;
     }
 }
Пример #18
0
 private BaseDatosException LanzarExcepcion(MySqlException exc)
 {
     switch (exc.Number)
     {
         case 1042:
             return new BaseDatosSinConexionException(exc.Message);
     }
     return new BaseDatosException(exc.Message);
 }
Пример #19
0
 public void errorWriter(MySqlException oMySQLException)
 {
     TheLogger.Log("ExecSql Error in " + oMySQLException.TargetSite + " due to : " + oMySQLException.Message + "\n");
     string errorText = oMySQLException.ToString();
     errorWriter(errorText);
 }
Пример #20
0
        /// <summary>
        /// Handles MySql exception.
        /// If WriteExceptionsToEventLog is set, will write exception info
        /// to event log. 
        /// It throws provider exception (original exception is stored as inner exception)
        /// </summary>
        /// <param name="e">exception</param>
        /// <param name="action"> name of the function that throwed the exception</param>
        private void HandleMySqlException(MySqlException e, string action)
        {
            if (WriteExceptionsToEventLog)
              {
            using (EventLog log = new EventLog())
            {
              log.Source = eventSource;
              log.Log = eventLog;

              string message = "An exception occurred communicating with the data source.\n\n";
              message += "Action: " + action;
              message += "Exception: " + e.ToString();
              log.WriteEntry(message);
            }
              }
              throw new ProviderException(exceptionMessage, e);
        }
        /// <summary>
        /// Turns all MySqlExceptions into DBExceptions.
        /// Error Codes: http://dev.mysql.com/doc/refman/5.0/en/error-messages-server.html
        /// </summary>
        /// <param Name="ex">A MySqlException to handle.</param>
        private void handleException(MySqlException ex)
        {
            //Console.WriteLine("ex.number:" + ex.Number);

            switch (ex.Number)
            {

                case 0:
                    //throw new DBException(ex.Message + ex.Number, ex);
                    throw new WrongCredentialsException(ex.Message, ex);
                case 1042:
                    throw new NoConnectionExpection(ex.Message, ex);
                case 1045:
                    throw new UnauthorizedAccessException(ex.Message, ex);

                default:
                    throw new DBException(ex.Message + ex.Number, ex);
            }
        }
Пример #22
0
 //Loggers
 #region MysqlLogger
 private void MySqlLogger(MySqlException me, string SqlQuery)
 {
     FileInfo t = new FileInfo("SqlError.log");
     if (t.Exists == true)
     {
         TextWriter tex = new StreamWriter(t.OpenWrite());
         tex.WriteLine("Date/Time: " + DateTime.Now.ToString());
         tex.WriteLine(" ");
         tex.WriteLine("Sql String: " + SqlQuery);
         tex.WriteLine(" ");
         tex.WriteLine("Sql Error: ");
         tex.WriteLine(me);
         tex.Write(tex.NewLine);
         tex.Flush();
         tex.Close();
         tex = null;
         t = null;
     }
     else
     {
         
         StreamWriter sw = t.CreateText();
         sw.WriteLine("Date/Time: " + DateTime.Now.ToString());
         sw.WriteLine(" ");
         sw.WriteLine("Sql String: " + SqlQuery);
         sw.WriteLine(" ");
         sw.WriteLine("Sql Error: ");
         sw.WriteLine(me);
         sw.Write(sw.NewLine);
         sw.Flush();
         sw.Close();
         sw = null;
         t = null;
     }
 }
Пример #23
0
		/// <summary>
		/// Handle Non Fatal SQL Query Exception
		/// </summary>
		/// <param name="sqle">SQL Excepiton</param>
		/// <returns>True if handled, False otherwise</returns>
		protected static bool HandleSQLException(MySqlException sqle)
		{
			switch ((MySqlErrorCode)sqle.Number)
			{
				case MySqlErrorCode.DuplicateUnique:
				case MySqlErrorCode.DuplicateKeyEntry:
					return true;
				default:
					return false;
			}
		}