Esempio n. 1
0
        private static SqlException CreateLocalDBException(string errorMessage, string instance = null, int localDbError = 0, int sniError = 0)
        {
            Debug.Assert((localDbError == 0) || (sniError == 0), "LocalDB error and SNI error cannot be specified simultaneously");
            Debug.Assert(!string.IsNullOrEmpty(errorMessage), "Error message should not be null or empty");
            SqlErrorCollection collection = new SqlErrorCollection();

            int errorCode = (localDbError == 0) ? sniError : localDbError;

            if (sniError != 0)
            {
                string sniErrorMessage = SQL.GetSNIErrorMessage(sniError);
                errorMessage = String.Format((IFormatProvider)null, "{0} (error: {1} - {2})",
                                             errorMessage, sniError, sniErrorMessage);
            }

            collection.Add(new SqlError(errorCode, 0, TdsEnums.FATAL_ERROR_CLASS, instance, errorMessage, null, 0));

            if (localDbError != 0)
            {
                collection.Add(new SqlError(errorCode, 0, TdsEnums.FATAL_ERROR_CLASS, instance, GetLocalDBMessage(localDbError), null, 0));
            }

            SqlException exc = SqlException.CreateException(collection, null);

            exc._doNotReconnect = true;

            return(exc);
        }
Esempio n. 2
0
        protected SqlException ProcessMessages(bool ignoreWarnings, bool ignoreNonFatalMessages)
        {
            SqlException       result = null;
            SqlErrorCollection temp   = null; // temp variable to store that which is being thrown - so that local copies can be deleted

            if (null != _errors)
            {
                Debug.Assert(0 != _errors.Count, "empty error collection?");   // must be something in the collection

                if (ignoreNonFatalMessages)
                {
                    temp = new SqlErrorCollection();
                    foreach (SqlError error in _errors)
                    {
                        if (error.Class >= TdsEnums.FATAL_ERROR_CLASS)
                        {
                            temp.Add(error);
                        }
                    }
                    if (temp.Count <= 0)
                    {
                        temp = null;
                    }
                }
                else
                {
                    if (null != _warnings)
                    {
                        // When we throw an exception we place all the warnings that
                        // occurred at the end of the collection - after all the errors.
                        // That way the user can see all the errors AND warnings that
                        // occurred for the exception.
                        foreach (SqlError warning in _warnings)
                        {
                            _errors.Add(warning);
                        }
                    }
                    temp = _errors;
                }

                _errors   = null;
                _warnings = null;
            }
            else
            {
                Debug.Assert(null == _warnings || 0 != _warnings.Count, "empty warning collection?");  // must be something in the collection

                if (!ignoreWarnings)
                {
                    temp = _warnings;
                }
                _warnings = null;
            }

            if (null != temp)
            {
                result = SqlException.CreateException(temp, ServerVersion);
            }
            return(result);
        }
Esempio n. 3
0
        /// <summary>
        /// SQLs the exception handling.
        /// </summary>
        /// <param name="exception">The exception.</param>
        /// <returns></returns>
        DbException IExceptionConverter.Convert(DbException exception)
        {
            var sqlException = (SqlException)exception;

            if (exception == null)
            {
                return(null);
            }

            var col     = new SqlErrorCollection();
            var message = "Unhandled Sql Exception: " + Environment.NewLine;

            foreach (System.Data.SqlClient.SqlError err in (sqlException.Errors))
            {
                var result = SqlErrorHandling(exception, err);
                if (result != null)
                {
                    return(result);
                }
                col.Add(err);

                message += err.Message + Environment.NewLine;
            }

            if (col.Count > 0)
            {
                return(new UnhandledSqlException(message, col, sqlException));
            }

            return(null);
        }
Esempio n. 4
0
        private static SqlException CreateLocalDBException(string errorMessage, string instance = null, int localDbError = 0, int sniError = 0)
        {
            SqlErrorCollection errorCollection = new SqlErrorCollection();
            int infoNumber = (localDbError == 0) ? sniError : localDbError;

            if (sniError != 0)
            {
                string name = string.Format(null, "SNI_ERROR_{0}", new object[] { sniError });
                errorMessage = string.Format(null, "{0} (error: {1} - {2})", new object[] { errorMessage, sniError, Res.GetString(name) });
            }
            errorCollection.Add(new SqlError(infoNumber, 0, 20, instance, errorMessage, null, 0));
            if (localDbError != 0)
            {
                errorCollection.Add(new SqlError(infoNumber, 0, 20, instance, GetLocalDBMessage(localDbError), null, 0));
            }
            SqlException exception = SqlException.CreateException(errorCollection, null);

            exception._doNotReconnect = true;
            return(exception);
        }
Esempio n. 5
0
        protected SqlException ProcessMessages(bool ignoreWarnings, bool ignoreNonFatalMessages)
        {
            SqlErrorCollection errorCollection = null;
            SqlException       exception       = null;

            if (this._errors != null)
            {
                if (ignoreNonFatalMessages)
                {
                    errorCollection = new SqlErrorCollection();
                    foreach (SqlError error in this._errors)
                    {
                        if (error.Class >= 20)
                        {
                            errorCollection.Add(error);
                        }
                    }
                    if (errorCollection.Count <= 0)
                    {
                        errorCollection = null;
                    }
                }
                else
                {
                    if (this._warnings != null)
                    {
                        foreach (SqlError error2 in this._warnings)
                        {
                            this._errors.Add(error2);
                        }
                    }
                    errorCollection = this._errors;
                }
                this._errors   = null;
                this._warnings = null;
            }
            else
            {
                if (!ignoreWarnings)
                {
                    errorCollection = this._warnings;
                }
                this._warnings = null;
            }
            if (errorCollection != null)
            {
                exception = SqlException.CreateException(errorCollection, this.ServerVersion);
            }
            return(exception);
        }