Esempio n. 1
0
        /// <summary>
        /// Executes a query
        /// </summary>
        /// <param name="args">Query string plus any pre-initialized parameters</param>
        /// <param name="initCommand">Lambda to add any necessary parameters or otherwise pre-process the command</param>
        /// <param name="readRow">Lambda to read a row.  All rows will be read unless true is returned</param>
        /// <param name="rowMode">Row mode - read all available rows or just a single row</param>
        /// <returns>True for success.  Sets LastError</returns>
        public bool ReadRows(DBHelperCommandArgs args, Action <MySqlCommand> initCommand, Action <MySqlDataReader> readRow, ReadRowMode rowMode)
        {
            if (args == null)
            {
                throw new ArgumentNullException(nameof(args));
            }
            if (readRow == null)
            {
                throw new ArgumentNullException(nameof(readRow));
            }

            bool fResult = true;

            using (MySqlCommand comm = new MySqlCommand())
            {
                InitCommandObject(comm, args);

                using (comm.Connection = new MySqlConnection(ConnectionString))
                {
                    LastError = string.Empty;

                    initCommand?.Invoke(comm);
                    try
                    {
                        comm.Connection.Open();
                        using (MySqlDataReader dr = comm.ExecuteReader())
                        {
                            if (dr.HasRows)
                            {
                                while (dr.Read())
                                {
                                    readRow(dr);
                                    if (rowMode == ReadRowMode.SingleRow)
                                    {
                                        break;
                                    }
                                }
                            }
                        }
                    }
                    catch (MySqlException ex)
                    {
                        throw new MyFlightbookException("MySQL error thrown in ReadRows; Query = " + CommandText ?? string.Empty, ex, string.Empty);
                    }
                    catch (MyFlightbookException ex)
                    {
                        LastError = ex.Message;
                        fResult   = false;
                    }
                    catch (Exception ex)
                    {
                        MyFlightbookException exNew = new MyFlightbookException(String.Format(CultureInfo.CurrentCulture, "Uncaught exception in ReadRows:\r\n:{0}", comm.CommandText), ex);
                        MyFlightbookException.NotifyAdminException(exNew);
                        throw exNew;
                    }
                }
            }
            return(fResult);
        }
Esempio n. 2
0
        /// <summary>
        /// Notify the user of an event, optionally Bcc admins.
        /// </summary>
        /// <param name="szSubject">The subject to send</param>
        /// <param name="szMessage">The message to send  This will be rebranded per Branding.ReBrand().</param>
        /// <param name="maUser">The address of the recipient</param>
        /// <param name="fCcAdmins">True if you want Admins CC'd; false if not</param>
        /// <param name="fIsHTML">True if the content of the message is HTML</param>
        /// <param name="brand">The branding to use</param>
        /// <param name="RoleMask">The roles to whom this should go (if admin)</param>
        static private void NotifyUser(string szSubject, string szMessage, MailAddress maUser, bool fCcAdmins, bool fIsHTML, Brand brand, uint RoleMask)
        {
            try
            {
                if (brand == null)
                {
                    brand = Branding.CurrentBrand;
                }

                if (szMessage == null)
                {
                    throw new ArgumentNullException(nameof(szMessage));
                }

                using (MailMessage msg = new MailMessage())
                {
                    msg.Subject    = szSubject;
                    msg.Body       = Branding.ReBrand(szMessage, brand);
                    msg.IsBodyHtml = fIsHTML || szMessage.Contains("<!DOCTYPE");

                    msg.From = new MailAddress(brand.EmailAddress, brand.AppName);

                    if (maUser != null)
                    {
                        msg.To.Add(maUser);
                    }

                    if (fCcAdmins)
                    {
                        AddAdminsToMessage(msg, (maUser == null), RoleMask);
                    }

                    SendMessage(msg);
                }
            }
            catch (ArgumentNullException ex)
            {
                MyFlightbookException mfbEx = new MyFlightbookException(String.Format(CultureInfo.CurrentCulture, "Null Argument in NotifyUser: {0}", ex.ParamName), ex);
                MyFlightbookException.NotifyAdminException(mfbEx);
            }
            catch (InvalidOperationException ex)
            {
                MyFlightbookException mfbEx = new MyFlightbookException("Invalid Operation in NotifyUser", ex);
                MyFlightbookException.NotifyAdminException(mfbEx);
            }
            catch (Exception ex) when(ex is SmtpException)
            {
                // Don't re-throw or do anything that would cause new mail to be sent because that could loop.  Just fail silently and eat this.
            }
        }