Ejemplo n.º 1
0
        /// <summary>
        ///     Converts the given CLR type into a DbType
        /// </summary>
        /// <param name="clrType"> The CLR type to convert </param>
        /// <returns> </returns>
        internal static DbType ConvertClrTypeToDbType(Type clrType)
        {
            switch (Type.GetTypeCode(clrType))
            {
            case TypeCode.Empty:
                throw new ArgumentException(Strings.ADP_InvalidDataType(TypeCode.Empty.ToString()));

            case TypeCode.Object:
                if (clrType == typeof(Byte[]))
                {
                    return(DbType.Binary);
                }
                if (clrType == typeof(Char[]))
                {
                    // Always treat char and char[] as string
                    return(DbType.String);
                }
                else if (clrType == typeof(Guid))
                {
                    return(DbType.Guid);
                }
                else if (clrType == typeof(TimeSpan))
                {
                    return(DbType.Time);
                }
                else if (clrType == typeof(DateTimeOffset))
                {
                    return(DbType.DateTimeOffset);
                }

                return(DbType.Object);

            case TypeCode.DBNull:
                return(DbType.Object);

            case TypeCode.Boolean:
                return(DbType.Boolean);

            case TypeCode.SByte:
                return(DbType.SByte);

            case TypeCode.Byte:
                return(DbType.Byte);

            case TypeCode.Char:
                // Always treat char and char[] as string
                return(DbType.String);

            case TypeCode.Int16:
                return(DbType.Int16);

            case TypeCode.UInt16:
                return(DbType.UInt16);

            case TypeCode.Int32:
                return(DbType.Int32);

            case TypeCode.UInt32:
                return(DbType.UInt32);

            case TypeCode.Int64:
                return(DbType.Int64);

            case TypeCode.UInt64:
                return(DbType.UInt64);

            case TypeCode.Single:
                return(DbType.Single);

            case TypeCode.Double:
                return(DbType.Double);

            case TypeCode.Decimal:
                return(DbType.Decimal);

            case TypeCode.DateTime:
                return(DbType.DateTime);

            case TypeCode.String:
                return(DbType.String);

            default:
                throw new ArgumentException(
                          Strings.ADP_UnknownDataTypeCode(
                              ((int)Type.GetTypeCode(clrType)).ToString(CultureInfo.InvariantCulture), clrType.FullName));
            }
        }