Example #1
0
        /// <summary>
        /// 判断一个数据库异常是否由于重复插入导致的,由唯一索引控制。
        /// </summary>
        /// <param name="ex"></param>
        /// <returns></returns>
        public static bool IsDuplicateInsert(this Exception ex)
        {
            if (ex == null)
            {
                throw new ArgumentNullException(nameof(ex));
            }

            SqlException sqlException = ex as SqlException;

            if (sqlException != null)
            {
                return(sqlException.Number == 2601);
            }

            // select * from master.dbo.sysmessages where error = 2601
            // error	severity	dlevel	description	                                                        msglangid
            // 2601	    14	        0	    不能在具有唯一索引“%2!”的对象“%1!”中插入重复键的行。重复键值为 %3!。	2052
            // For example: 不能在具有唯一索引“IX_Table1_IntValue”的对象“dbo.Table1”中插入重复键的行。重复键值为 (31)。

            DbExceuteException dbExceuteException = ex as DbExceuteException;

            if (dbExceuteException != null)
            {
                SqlException sqlException2 = dbExceuteException.InnerException as SqlException;
                if (sqlException2 != null)
                {
                    return(sqlException2.Number == 2601);
                }
            }

            return(false);
        }
Example #2
0
        /// <summary>
        /// Error事件处理方法
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void App_Error(object sender, EventArgs e)
        {
            HttpApplication app = (HttpApplication)sender;
            Exception       ex  = app.Server.GetLastError();

            if (ex != null)
            {
                DbCommand dbCommand = null;

                DbExceuteException dbExceuteException = ex as DbExceuteException;
                if (dbExceuteException != null)
                {
                    dbCommand = dbExceuteException.Command;
                }

                ExceptionInfo exceptionInfo = ExceptionInfo.Create(ex, app.Context, dbCommand);
                LogHelper.Write(exceptionInfo);
            }
        }