示例#1
0
        /// <summary>
        /// Retrieves information about a database that the application is currently connected to.
        /// </summary>
        /// <param name="databaseManager">Database manager object that handles the connection to
        /// the database.</param>
        /// <param name="commonDatabaseErrorCode">Structure holding the common database-related
        /// error codes.</param>
        /// <param name="oErrorInfo">Output parameter: Details of any error that may have
        /// occurred.</param>
        /// <returns>A structure containing the database metadata.</returns>
        public static DatabaseInfo GetDatabaseInfo <T>(DatabaseManager2 databaseManager,
                                                       CommonDatabaseErrorCode <T> commonDatabaseErrorCode, out ErrorInfo <T> oErrorInfo)
            where T : IComparable, IFormattable, IConvertible
        {
            DatabaseInfo  dbInfo    = new DatabaseInfo();
            ErrorInfo <T> errorInfo = new ErrorInfo <T>();

            lock (_lockGetDatabaseInfo)
            {
                try
                {
                    errorInfo.ErrorCode = commonDatabaseErrorCode.GeneralError;
                    errorInfo.Message   = string.Empty;
                    // Cannot convert generic type to int directly so cheat by using object.
                    object value = commonDatabaseErrorCode.Success;
                    errorInfo.DatabaseErrorValue = (int)value;

                    DataTable dbMetaData = databaseManager.GetDataTable("p_GetDatabaseInfo",
                                                                        out errorInfo.DatabaseErrorValue, out errorInfo.Message);

                    // Check for unrecognised error code from the database.
                    errorInfo.ErrorCode = BBError.ValidateDBErrorValue <T>(
                        errorInfo.DatabaseErrorValue, commonDatabaseErrorCode.GeneralDBError);

                    if (dbMetaData != null && dbMetaData.Rows.Count > 0)
                    {
                        dbInfo.DatabaseTitle   = (dbMetaData.Rows[0]["DatabaseTitle"]).ToString();
                        dbInfo.DatabaseVersion = (int)dbMetaData.Rows[0]["DatabaseVersion"];

                        // Check that database type is valid.
                        string       databaseTypeText = (dbMetaData.Rows[0]["DatabaseType"]).ToString();
                        DatabaseType databaseType     = DatabaseType.NotFound;
                        if (MiscUtilities.ValidateEnumValue <DatabaseType>(databaseTypeText,
                                                                           out databaseType))
                        {
                            dbInfo.DatabaseType = databaseType;
                            errorInfo.ErrorCode = commonDatabaseErrorCode.Success;
                        }
                        else
                        {
                            dbInfo.DatabaseType = DatabaseType.Invalid;
                            errorInfo.ErrorCode = commonDatabaseErrorCode.InvalidType;
                        }
                    }
                    else
                    {
                        dbInfo.DatabaseType = DatabaseType.NotFound;
                        errorInfo.ErrorCode = commonDatabaseErrorCode.NoInfo;
                    }
                }
                catch
                {
                    errorInfo.ErrorCode = commonDatabaseErrorCode.GeneralError;
                }
            }

            oErrorInfo = errorInfo;
            return(dbInfo);
        }
示例#2
0
        /// <summary>
        /// Checks to see if the error value returned from a call to the database is a valid
        /// error code.
        /// </summary>
        /// <typeparam name="TErrorCodeEnum">Type of the error code enum that the database error
        /// value should be a member of.</typeparam>
        /// <param name="dbErrorValue">Error value returned from a call to the database.</param>
        /// <param name="defaultErrorCode">Error code that is returned if the database error
        /// value is not valid.</param>
        /// <returns>Error code enum.  If the database error value is valid it is converted to
        /// an error code enum.  If it is not valid the default error code is returned.</returns>
        public static TErrorCodeEnum ValidateDBErrorValue <TErrorCodeEnum>(int dbErrorValue,
                                                                           TErrorCodeEnum defaultErrorCode)
            where TErrorCodeEnum : IComparable, IFormattable, IConvertible
        {
            lock (_lockValidateDBErrorValue)
            {
                TErrorCodeEnum errorCode = default(TErrorCodeEnum);
                if (MiscUtilities.ValidateEnumValue <TErrorCodeEnum>(dbErrorValue, out errorCode))
                {
                    return(errorCode);
                }

                return(defaultErrorCode);
            }
        }