CoerceValue() static private method

static private CoerceValue ( object value, MetaType destinationType, bool &coercedToDataFeed, bool &typeChanged, bool allowStreaming = true ) : object
value object
destinationType MetaType
coercedToDataFeed bool
typeChanged bool
allowStreaming bool
return object
        private object ConvertValue(object value, _SqlMetaData metadata)
        {
            object obj2;

            if (ADP.IsNull(value))
            {
                if (!metadata.isNullable)
                {
                    throw SQL.BulkLoadBulkLoadNotAllowDBNull(metadata.column);
                }
                return(value);
            }
            MetaType metaType = metadata.metaType;

            try
            {
                MetaType   metaTypeFromSqlDbType;
                SqlDecimal num2;
                switch (metaType.NullableType)
                {
                case 0x22:
                case 0x23:
                case 0x24:
                case 0x26:
                case 40:
                case 0x29:
                case 0x2a:
                case 0x2b:
                case 50:
                case 0x3a:
                case 0x3b:
                case 0x3d:
                case 0x3e:
                case 0x68:
                case 0x6d:
                case 110:
                case 0x6f:
                case 0xa5:
                case 0xa7:
                case 0xad:
                case 0xaf:
                    metaTypeFromSqlDbType = MetaType.GetMetaTypeFromSqlDbType(metaType.SqlDbType, false);
                    value = SqlParameter.CoerceValue(value, metaTypeFromSqlDbType);
                    goto Label_0290;

                case 0x62:
                    value = this.ValidateBulkCopyVariant(value);
                    goto Label_0290;

                case 0x63:
                case 0xef:
                case 0xe7:
                {
                    metaTypeFromSqlDbType = MetaType.GetMetaTypeFromSqlDbType(metaType.SqlDbType, false);
                    value = SqlParameter.CoerceValue(value, metaTypeFromSqlDbType);
                    int num3 = (value is string) ? ((string)value).Length : ((SqlString)value).Value.Length;
                    if (num3 > (metadata.length / 2))
                    {
                        throw SQL.BulkLoadStringTooLong();
                    }
                    goto Label_0290;
                }

                case 0x6a:
                case 0x6c:
                    metaTypeFromSqlDbType = MetaType.GetMetaTypeFromSqlDbType(metaType.SqlDbType, false);
                    value = SqlParameter.CoerceValue(value, metaTypeFromSqlDbType);
                    if (!(value is SqlDecimal))
                    {
                        break;
                    }
                    num2 = (SqlDecimal)value;
                    goto Label_017D;

                case 240:
                    if (value.GetType() != typeof(byte[]))
                    {
                        value = this._connection.GetBytes(value);
                    }
                    goto Label_0290;

                case 0xf1:
                    if (value is XmlReader)
                    {
                        value = MetaType.GetStringFromXml((XmlReader)value);
                    }
                    goto Label_0290;

                default:
                    throw SQL.BulkLoadCannotConvertValue(value.GetType(), metadata.metaType, null);
                }
                num2 = new SqlDecimal((decimal)value);
Label_017D:
                if (num2.Scale != metadata.scale)
                {
                    num2  = TdsParser.AdjustSqlDecimalScale(num2, metadata.scale);
                    value = num2;
                }
                if (num2.Precision > metadata.precision)
                {
                    throw SQL.BulkLoadCannotConvertValue(value.GetType(), metaTypeFromSqlDbType, ADP.ParameterValueOutOfRange(num2));
                }
Label_0290:
                obj2 = value;
            }
            catch (Exception exception)
            {
                if (!ADP.IsCatchableExceptionType(exception))
                {
                    throw;
                }
                throw SQL.BulkLoadCannotConvertValue(value.GetType(), metadata.metaType, exception);
            }
            return(obj2);
        }