Exemplo n.º 1
0
        /// <summary>
        /// 지정된 값을 지정된 <see cref="DbType"/>과 같은 형식으로 변환한다.
        /// </summary>
        /// <param name="dbType">원하는 <see cref="DbType"/></param>
        /// <param name="value">변경할 값</param>
        /// <returns>지정된 <see cref="DbType"/>으로 변경된 값</returns>
        public static object ConvertToDbTypedValue(this DbType dbType, object value)
        {
            // value 가 null이면 Db형식에 상관없이 NULL값을 반환한다.
            if (Equals(value, null))
            {
                return(DBNull.Value);
            }

            dbType.ShouldNotBeNull("dbType");

            try {
                switch (dbType)
                {
                case DbType.Time:
                    return(value.AsTimeSpanNullable());

                case DbType.Date:
                case DbType.DateTime:
                case DbType.DateTime2:
                    return(value.AsDateTimeNullable());

                case DbType.Int16:
                    return(value.AsShortNullable());

                case DbType.Int32:
                    return(value.AsIntNullable());

                case DbType.Int64:
                    return(value.AsLongNullable());

                case DbType.Decimal:
                    return(value.AsDecimalNullable());

                case DbType.Currency:
                case DbType.Double:
                    return(value.AsDoubleNullable());

                case DbType.Single:
                    return(value.AsFloatNullable());

                case DbType.Guid:
                    return(value.AsGuidNullable());

                case DbType.Byte:
                    return(value.AsByteNullable());

                case DbType.SByte:
                    return(value.AsValue <sbyte>());


                case DbType.String:
                case DbType.StringFixedLength:
                    return(value.AsText());

                default:
                    return(value);
                }
            }
            catch (Exception ex) {
                if (log.IsErrorEnabled)
                {
                    log.Error("Cannot Parse value to proper DbType=[{0}]", dbType);
                    log.Error(ex);
                }
                throw;
            }
        }