/// <summary> /// 附加在命令发生 SqlException 异常时,直接提示用户的行为。 /// </summary> public static void AttachAlertCommandSqlErrorBehavior() { CommandRepository.CommandCreated += (o, e) => { e.Instance.ExecuteFailed += (oo, ee) => { if (!ee.Cancel) { //如果出现了已知的 SqlException,直接弹出 SqlException 的提示。 var sqlex = ee.Exception.GetBaseException() as SqlException; if (sqlex != null) { var sqlerr = SqlErrorInfo.GetSqlError(sqlex.Number); if (sqlerr == null) { return; } App.MessageBox.Show(sqlerr.ErrorMessage.Translate()); ee.Cancel = true; } } }; }; }
public override void Initialize(IApp app) { AddResources(); SetupWPFCommands(); SqlErrorInfo.AttachAlertCommandSqlErrorBehavior(); }
/// <summary> /// 显示一些已知的异常。 /// </summary> /// <param name="ex"></param> private static void ShowKnownException(Exception ex) { var baseException = ex.GetBaseException(); string msg = null; if (baseException is SqlException) { var sqlEx = baseException as SqlException; var sqlErr = SqlErrorInfo.GetSqlError(sqlEx.Number); if (sqlErr != null) { msg = sqlErr.ErrorMessage; } else { msg = "数据库访问出现异常。"; } } else if (baseException is DbException) { msg = "数据库访问出现异常。"; } else if (baseException is TimeoutException) { msg = "网络连接超时,请检查网络连接是否正常"; } else if (baseException is Win32Exception || baseException is EndpointNotFoundException) { msg = "网络连接出现异常,请检查连接地址是否正确或连接是否正常"; } msg = msg ?? @"遇到未知错误,我们对此引起的不便表示抱歉。系统已经产生了一个关于此错误的报告,希望您将问题反馈给我们以帮助改善质量。"; App.MessageBox.Show(msg.Translate(), "异常提示".Translate(), MessageBoxButton.OK, MessageBoxImage.Exclamation); }