System.Data.SqlClient.SqlParameter.CoerceValue C# (CSharp) Méthode

CoerceValue() static private méthode

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
Résultat object
        internal static object CoerceValue(object value, MetaType destinationType, out bool coercedToDataFeed, out bool typeChanged, bool allowStreaming = true)
        {
            Debug.Assert(!(value is DataFeed), "Value provided should not already be a data feed");
            Debug.Assert(!ADP.IsNull(value), "Value provided should not be null");
            Debug.Assert(null != destinationType, "null destinationType");

            coercedToDataFeed = false;
            typeChanged = false;
            Type currentType = value.GetType();

            if ((typeof(object) != destinationType.ClassType) &&
                    (currentType != destinationType.ClassType) &&
                    ((currentType != destinationType.SqlType) || (SqlDbType.Xml == destinationType.SqlDbType)))
            {   // Special case for Xml types (since we need to convert SqlXml into a string)
                try
                {
                    // Assume that the type changed
                    typeChanged = true;
                    if ((typeof(string) == destinationType.ClassType))
                    {
                        // For Xml data, destination Type is always string
                        if (typeof(SqlXml) == currentType)
                        {
                            value = MetaType.GetStringFromXml((XmlReader)(((SqlXml)value).CreateReader()));
                        }
                        else if (typeof(SqlString) == currentType)
                        {
                            typeChanged = false;   // Do nothing
                        }
                        else if (typeof(XmlReader).IsAssignableFrom(currentType))
                        {
                            if (allowStreaming)
                            {
                                coercedToDataFeed = true;
                                value = new XmlDataFeed((XmlReader)value);
                            }
                            else
                            {
                                value = MetaType.GetStringFromXml((XmlReader)value);
                            }
                        }
                        else if (typeof(char[]) == currentType)
                        {
                            value = new string((char[])value);
                        }
                        else if (typeof(SqlChars) == currentType)
                        {
                            value = new string(((SqlChars)value).Value);
                        }
                        else if (value is TextReader && allowStreaming)
                        {
                            coercedToDataFeed = true;
                            value = new TextDataFeed((TextReader)value);
                        }
                        else
                        {
                            value = Convert.ChangeType(value, destinationType.ClassType, (IFormatProvider)null);
                        }
                    }
                    else if ((DbType.Currency == destinationType.DbType) && (typeof(string) == currentType))
                    {
                        value = Decimal.Parse((string)value, NumberStyles.Currency, (IFormatProvider)null); // WebData 99376
                    }
                    else if ((typeof(SqlBytes) == currentType) && (typeof(byte[]) == destinationType.ClassType))
                    {
                        typeChanged = false;    // Do nothing
                    }
                    else if ((typeof(string) == currentType) && (SqlDbType.Time == destinationType.SqlDbType))
                    {
                        value = TimeSpan.Parse((string)value);
                    }
                    else if ((typeof(string) == currentType) && (SqlDbType.DateTimeOffset == destinationType.SqlDbType))
                    {
                        value = DateTimeOffset.Parse((string)value, (IFormatProvider)null);
                    }
                    else if ((typeof(DateTime) == currentType) && (SqlDbType.DateTimeOffset == destinationType.SqlDbType))
                    {
                        value = new DateTimeOffset((DateTime)value);
                    }
                    else if (TdsEnums.SQLTABLE == destinationType.TDSType && (
                                value is DbDataReader ||
                                value is System.Collections.Generic.IEnumerable<SqlDataRecord>))
                    {
                        // no conversion for TVPs.
                        typeChanged = false;
                    }
                    else if (destinationType.ClassType == typeof(byte[]) && value is Stream && allowStreaming)
                    {
                        coercedToDataFeed = true;
                        value = new StreamDataFeed((Stream)value);
                    }
                    else
                    {
                        value = Convert.ChangeType(value, destinationType.ClassType, (IFormatProvider)null);
                    }
                }
                catch (Exception e)
                {
                    if (!ADP.IsCatchableExceptionType(e))
                    {
                        throw;
                    }

                    throw ADP.ParameterConversionFailed(value, destinationType.ClassType, e); // WebData 75433
                }
            }

            Debug.Assert(allowStreaming || !coercedToDataFeed, "Streaming is not allowed, but type was coerced into a data feed");
            Debug.Assert(value.GetType() == currentType ^ typeChanged, "Incorrect value for typeChanged");
            return value;
        }

Usage Example

        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);
        }