private static MetaType GetMetaTypeFromValue(Type dataType, object value, bool inferLen) { switch (Type.GetTypeCode(dataType)) { case TypeCode.Empty: throw ADP.InvalidDataType(TypeCode.Empty); case TypeCode.Object: if (!(dataType == typeof(byte[]))) { if (dataType == typeof(Guid)) { return(MetaUniqueId); } if (dataType == typeof(object)) { return(MetaVariant); } if (dataType == typeof(SqlBinary)) { return(MetaVarBinary); } if (dataType == typeof(SqlBoolean)) { return(MetaBit); } if (dataType == typeof(SqlByte)) { return(MetaTinyInt); } if (dataType == typeof(SqlBytes)) { return(MetaVarBinary); } if (dataType != typeof(SqlChars)) { if (dataType == typeof(SqlDateTime)) { return(MetaDateTime); } if (dataType == typeof(SqlDouble)) { return(MetaFloat); } if (dataType == typeof(SqlGuid)) { return(MetaUniqueId); } if (dataType == typeof(SqlInt16)) { return(MetaSmallInt); } if (dataType == typeof(SqlInt32)) { return(MetaInt); } if (dataType == typeof(SqlInt64)) { return(MetaBigInt); } if (dataType == typeof(SqlMoney)) { return(MetaMoney); } if (dataType == typeof(SqlDecimal)) { return(MetaDecimal); } if (dataType == typeof(SqlSingle)) { return(MetaReal); } if (dataType == typeof(SqlXml)) { return(MetaXml); } if (dataType == typeof(XmlReader)) { return(MetaXml); } if (!(dataType == typeof(SqlString))) { if ((dataType == typeof(IEnumerable <DbDataRecord>)) || (dataType == typeof(DataTable))) { return(MetaTable); } if (dataType == typeof(TimeSpan)) { return(MetaTime); } if (dataType == typeof(DateTimeOffset)) { return(MetaDateTimeOffset); } if (SqlUdtInfo.TryGetFromType(dataType) == null) { throw ADP.UnknownDataType(dataType); } return(MetaUdt); } if (inferLen) { SqlString str2 = (SqlString)value; if (!str2.IsNull) { SqlString str = (SqlString)value; return(PromoteStringType(str.Value)); } } } return(MetaNVarChar); } if (inferLen && (((byte[])value).Length > 0x1f40)) { return(MetaImage); } return(MetaVarBinary); case TypeCode.DBNull: throw ADP.InvalidDataType(TypeCode.DBNull); case TypeCode.Boolean: return(MetaBit); case TypeCode.Char: throw ADP.InvalidDataType(TypeCode.Char); case TypeCode.SByte: throw ADP.InvalidDataType(TypeCode.SByte); case TypeCode.Byte: return(MetaTinyInt); case TypeCode.Int16: return(MetaSmallInt); case TypeCode.UInt16: throw ADP.InvalidDataType(TypeCode.UInt16); case TypeCode.Int32: return(MetaInt); case TypeCode.UInt32: throw ADP.InvalidDataType(TypeCode.UInt32); case TypeCode.Int64: return(MetaBigInt); case TypeCode.UInt64: throw ADP.InvalidDataType(TypeCode.UInt64); case TypeCode.Single: return(MetaReal); case TypeCode.Double: return(MetaFloat); case TypeCode.Decimal: return(MetaDecimal); case TypeCode.DateTime: return(MetaDateTime); case TypeCode.String: if (!inferLen) { return(MetaNVarChar); } return(PromoteStringType((string)value)); } throw ADP.UnknownDataTypeCode(dataType, Type.GetTypeCode(dataType)); }
private static MetaType GetMetaTypeFromValue(Type dataType, object value, bool inferLen, bool streamAllowed) { switch (Type.GetTypeCode(dataType)) { case TypeCode.Empty: throw ADP.InvalidDataType(TypeCode.Empty); case TypeCode.Object: if (dataType == typeof(byte[])) { // Must not default to image if inferLen is false if (!inferLen || ((byte[])value).Length <= TdsEnums.TYPE_SIZE_LIMIT) { return(MetaVarBinary); } else { return(MetaImage); } } else if (dataType == typeof(System.Guid)) { return(s_metaUniqueId); } else if (dataType == typeof(object)) { return(s_metaVariant); } // check sql types now else if (dataType == typeof(SqlBinary)) { return(MetaVarBinary); } else if (dataType == typeof(SqlBoolean)) { return(s_metaBit); } else if (dataType == typeof(SqlByte)) { return(s_metaTinyInt); } else if (dataType == typeof(SqlBytes)) { return(MetaVarBinary); } else if (dataType == typeof(SqlChars)) { return(MetaNVarChar); } else if (dataType == typeof(SqlDateTime)) { return(s_metaDateTime); } else if (dataType == typeof(SqlDouble)) { return(s_metaFloat); } else if (dataType == typeof(SqlGuid)) { return(s_metaUniqueId); } else if (dataType == typeof(SqlInt16)) { return(s_metaSmallInt); } else if (dataType == typeof(SqlInt32)) { return(s_metaInt); } else if (dataType == typeof(SqlInt64)) { return(s_metaBigInt); } else if (dataType == typeof(SqlMoney)) { return(s_metaMoney); } else if (dataType == typeof(SqlDecimal)) { return(MetaDecimal); } else if (dataType == typeof(SqlSingle)) { return(s_metaReal); } else if (dataType == typeof(SqlXml)) { return(MetaXml); } else if (dataType == typeof(SqlString)) { return((inferLen && !((SqlString)value).IsNull) ? PromoteStringType(((SqlString)value).Value) : MetaNVarChar); } else if (dataType == typeof(IEnumerable <DbDataRecord>) || dataType == typeof(DataTable)) { return(s_metaTable); } else if (dataType == typeof(TimeSpan)) { return(MetaTime); } else if (dataType == typeof(DateTimeOffset)) { return(MetaDateTimeOffset); } else { // UDT ? SqlUdtInfo attribs = SqlUdtInfo.TryGetFromType(dataType); if (attribs != null) { return(MetaUdt); } if (streamAllowed) { // Derived from Stream ? if (typeof(Stream).IsAssignableFrom(dataType)) { return(MetaVarBinary); } // Derived from TextReader ? else if (typeof(TextReader).IsAssignableFrom(dataType)) { return(MetaNVarChar); } // Derived from XmlReader ? else if (typeof(System.Xml.XmlReader).IsAssignableFrom(dataType)) { return(MetaXml); } } } throw ADP.UnknownDataType(dataType); case TypeCode.DBNull: throw ADP.InvalidDataType(TypeCode.DBNull); case TypeCode.Boolean: return(s_metaBit); case TypeCode.Char: throw ADP.InvalidDataType(TypeCode.Char); case TypeCode.SByte: throw ADP.InvalidDataType(TypeCode.SByte); case TypeCode.Byte: return(s_metaTinyInt); case TypeCode.Int16: return(s_metaSmallInt); case TypeCode.UInt16: throw ADP.InvalidDataType(TypeCode.UInt16); case TypeCode.Int32: return(s_metaInt); case TypeCode.UInt32: throw ADP.InvalidDataType(TypeCode.UInt32); case TypeCode.Int64: return(s_metaBigInt); case TypeCode.UInt64: throw ADP.InvalidDataType(TypeCode.UInt64); case TypeCode.Single: return(s_metaReal); case TypeCode.Double: return(s_metaFloat); case TypeCode.Decimal: return(MetaDecimal); case TypeCode.DateTime: return(s_metaDateTime); case TypeCode.String: return(inferLen ? PromoteStringType((string)value) : MetaNVarChar); default: throw ADP.UnknownDataTypeCode(dataType, Type.GetTypeCode(dataType)); } }