public static void RegisterCallback(int index, IntPtr function) { switch (index) { case 11: connected = (IdeConnected)Marshal.GetDelegateForFunctionPointer(function, typeof(IdeConnected)); break; case 12: getConnectionInfo = (IdeGetConnectionInfo)Marshal.GetDelegateForFunctionPointer(function, typeof(IdeGetConnectionInfo)); break; case 40: sqlExecute = (SqlExecute)Marshal.GetDelegateForFunctionPointer(function, typeof(SqlExecute)); break; case 42: sqlEof = (SqlEof)Marshal.GetDelegateForFunctionPointer(function, typeof(SqlEof)); break; case 43: sqlNext = (SqlNext)Marshal.GetDelegateForFunctionPointer(function, typeof(SqlNext)); break; case 44: sqlField = (SqlField)Marshal.GetDelegateForFunctionPointer(function, typeof(SqlField)); break; case 48: sqlErrorMessage = (SqlErrorMessage)Marshal.GetDelegateForFunctionPointer(function, typeof(SqlErrorMessage)); break; case 69: createPopupItem = (IdeCreatePopupItem)Marshal.GetDelegateForFunctionPointer(function, typeof(IdeCreatePopupItem)); break; case 74: getPopupObject = (IdeGetPopupObject)Marshal.GetDelegateForFunctionPointer(function, typeof(IdeGetPopupObject)); break; case 150: createToolButton = (IdeCreateToolButton)Marshal.GetDelegateForFunctionPointer(function, typeof(IdeCreateToolButton)); break; } }
/// <summary> /// Prend en charge une exception Sql Server. /// </summary> /// <param name="exception">Exception.</param> /// <returns>Exception.</returns> public Exception HandleException(DbException exception) { Analytics.Instance.IncValue(SqlServerManager.CounterSqlErrorCount, 1); SqlException sqlException = exception as SqlException; if (sqlException == null) { return(new SqlServerException(exception.Message, exception)); } SqlErrorMessage message = null; ILog log = LogManager.GetLogger(string.IsNullOrEmpty(_command.LogManagerName) ? "Sql" : _command.LogManagerName); foreach (SqlError error in sqlException.Errors) { if (log.IsErrorEnabled) { log.ErrorFormat( CultureInfo.CurrentCulture, "Error class:{0} message:{1} line:{2} number:{3} proc:{4} server:{5} source:{6} state:{7}", error.Class, error.Message, error.LineNumber, error.Number, error.Procedure, error.Server, error.Source, error.State); } if (error.Number == 1205) { // Erreur de deadlock. Analytics.Instance.IncValue(SqlServerManager.CounterSqlDeadLockCount, 1); } else if (error.Class == TimeOutErrorClass && (error.Number == TimeOutErrorCode1 || error.Number == TimeOutErrorCode2)) { // Erreur de timeout. Analytics.Instance.IncValue(SqlServerManager.CounterSqlTimeoutCount, 1); return(new SqlServerTimeoutException(exception.Message, exception)); } else if (error.Class == 16 && error.Number == 547) { // Erreur de contrainte. message = HandleConstraintException(error.Message); } else if (error.Class == 14 && error.Number == 2601) { // Erreur de contrainte. message = HandleUniqueConstraintException(error.Message); } else if (error.Class == 14 && error.Number == 2627) { // Erreur de contrainte. message = HandleUniqueConstraintException(error.Message); } } if (message != null) { return(new ComponentModel.ConstraintException(message.Message, message.Code, exception)); } return(new SqlServerException(exception.Message, exception)); }
/// <summary> /// 数据库事物处理 /// 自动增长列的表,自动处理外键赋值 /// 执行的队列必须严格按照顺序添加入队列 /// 目前不支持单表多个主键 /// </summary> /// <param name="connectionString"></param> /// <param name="exeQueue">执行队列,自动增长列的表,外键不需要赋值</param> /// <returns></returns> public static SqlErrorMessage BeginTransaction(string connectionString, Queue <SqlTransactionParameter> exeQueue, ref List <TablePrimaryKeyValue> tablePkValue) { SqlErrorMessage errorMessage = new SqlErrorMessage(); SqlConnection conn = null; SqlTransaction transaction = null; try { var index = 0; // 外键数据集合 Dictionary <string, object> FKValues = new Dictionary <string, object>(); conn = new SqlConnection(connectionString); conn.Open(); transaction = conn.BeginTransaction(IsolationLevel.ReadCommitted); var stp = exeQueue.Dequeue(); while (stp != null) { errorMessage.CommonText = stp.CommonText; errorMessage.Parameters = stp.Parameters; var sqlCommand = new SqlCommand(stp.CommonText, conn, transaction); sqlCommand.CommandType = CommandType.Text; // 自动增长列的保存主键,用于其他执行的外键赋值 if (stp.PkIsIdentity && !stp.IsUpdate) { if (tablePkValue == null) { tablePkValue = new List <TablePrimaryKeyValue>(); } sqlCommand.CommandText = sqlCommand.CommandText + ";SELECT SCOPE_IDENTITY()"; foreach (var fk in FKValues) { var pFK = stp.Parameters.Find(t => t.ParameterName == "@" + fk.Key); if (pFK != null) { pFK.Value = fk.Value; } } if (stp.Parameters.Count > 0) { sqlCommand.Parameters.AddRange(stp.Parameters.ToArray()); } var resultPK = sqlCommand.ExecuteScalar(); // 如果外键名称不为空的使用外键名称 if (!FKValues.ContainsKey(string.IsNullOrEmpty(stp.FKName) ? stp.PK : stp.FKName)) { FKValues.Add(string.IsNullOrEmpty(stp.FKName) ? stp.PK : stp.FKName, resultPK); } tablePkValue.Add(new TablePrimaryKeyValue() { TableName = stp.TableName, PkName = stp.PK, PkValue = resultPK, TableIndex = index }); index++; } else { if (stp.Parameters.Count > 0) { sqlCommand.Parameters.AddRange(stp.Parameters.ToArray()); } sqlCommand.ExecuteNonQuery(); } if (exeQueue.Count == 0) { break; } stp = exeQueue.Dequeue(); } transaction.Commit(); FKValues.Clear(); } catch (Exception ex) { if (transaction != null) { transaction.Rollback(); } errorMessage.IsError = true; errorMessage.ErrorMessage = ex.Message; errorMessage.Ex = ex; throw ex; } finally { if (transaction != null) { transaction = null; } if (conn != null) { conn.Close(); } } return(errorMessage); }