Beispiel #1
0
        public static SqlInt16 ConvertToSqlInt16(object value)
        {
            if (value == DBNull.Value)
            {
                return(SqlInt16.Null);
            }
            Type type = value.GetType();

            switch (DataStorageHelper.GetStorageType(type))
            {
            case StorageType.Byte:
                return((SqlInt16)((short)(byte)value));

            case StorageType.Int16:
                return((SqlInt16)((short)value));

            case StorageType.SqlByte:
                return((SqlInt16)((SqlByte)value));

            case StorageType.SqlInt16:
                return((SqlInt16)value);

            default:
                throw InvalidExpressionException.SqlConvertFailed(type, typeof(SqlInt16));
            }
        }
Beispiel #2
0
        public static SqlInt32 ConvertToSqlInt32(object value)
        {
            if (value == DBNull.Value)
            {
                return(SqlInt32.Null);
            }
            Type dataType = value.GetType();

            switch (DataStorageHelper.GetStorageType(dataType))
            {
            case StorageType.Byte:
                return((SqlInt32)((byte)value));

            case StorageType.Int16:
                return((SqlInt32)((short)value));

            case StorageType.UInt16:
                return((SqlInt32)((ushort)value));

            case StorageType.Int32:
                return((int)value);

            case StorageType.SqlByte:
                return((SqlByte)value);

            case StorageType.SqlInt16:
                return((SqlInt16)value);

            case StorageType.SqlInt32:
                return((SqlInt32)value);
            }
            throw InvalidExpressionException.SqlConvertFailed(dataType, typeof(SqlInt32));
        }
Beispiel #3
0
        public static SqlSingle ConvertToSqlSingle(object value)
        {
            if (value == DBNull.Value)
            {
                return(SqlSingle.Null);
            }
            Type type = value.GetType();

            switch (DataStorageHelper.GetStorageType(type))
            {
            case StorageType.Byte:
                return((SqlSingle)((float)(byte)value));

            case StorageType.Int16:
                return((SqlSingle)((float)(short)value));

            case StorageType.UInt16:
                return((SqlSingle)((float)(ushort)value));

            case StorageType.Int32:
                return((SqlSingle)((float)(int)value));

            case StorageType.UInt32:
                return((SqlSingle)((float)(uint)value));

            case StorageType.Int64:
                return((SqlSingle)((float)(long)value));

            case StorageType.UInt64:
                return((SqlSingle)((float)(ulong)value));

            case StorageType.Single:
                return((SqlSingle)((float)value));

            case StorageType.SqlByte:
                return((SqlSingle)((SqlByte)value));

            case StorageType.SqlDecimal:
                return((SqlSingle)((SqlDecimal)value));

            case StorageType.SqlInt16:
                return((SqlSingle)((SqlInt16)value));

            case StorageType.SqlInt32:
                return((SqlSingle)((SqlInt32)value));

            case StorageType.SqlInt64:
                return((SqlSingle)((SqlInt64)value));

            case StorageType.SqlMoney:
                return((SqlSingle)((SqlMoney)value));

            case StorageType.SqlSingle:
                return((SqlSingle)value);

            default:
                throw InvalidExpressionException.SqlConvertFailed(type, typeof(SqlSingle));
            }
        }
Beispiel #4
0
        public static SqlChars ConvertToSqlChars(object value)
        {
            if (value == DBNull.Value)
            {
                return(SqlChars.Null);
            }
            Type dataType = value.GetType();

            if (DataStorageHelper.GetStorageType(dataType) != StorageType.SqlChars)
            {
                throw InvalidExpressionException.SqlConvertFailed(dataType, typeof(SqlChars));
            }
            return((SqlChars)value);
        }
Beispiel #5
0
        public static SqlGuid ConvertToSqlGuid(object value)
        {
            if (value == DBNull.Value)
            {
                return(SqlGuid.Null);
            }
            Type        dataType    = value.GetType();
            StorageType storageType = DataStorageHelper.GetStorageType(dataType);

            if (storageType != StorageType.Guid)
            {
                if (storageType != StorageType.SqlGuid)
                {
                    throw InvalidExpressionException.SqlConvertFailed(dataType, typeof(SqlGuid));
                }
                return((SqlGuid)value);
            }
            return((Guid)value);
        }
Beispiel #6
0
        public static SqlBoolean ConvertToSqlBoolean(object value)
        {
            if ((value == DBNull.Value) || (value == null))
            {
                return(SqlBoolean.Null);
            }
            Type        dataType    = value.GetType();
            StorageType storageType = DataStorageHelper.GetStorageType(dataType);

            if (storageType != StorageType.Boolean)
            {
                if (storageType != StorageType.SqlBoolean)
                {
                    throw InvalidExpressionException.SqlConvertFailed(dataType, typeof(SqlBoolean));
                }
                return((SqlBoolean)value);
            }
            return((bool)value);
        }
Beispiel #7
0
        public static SqlBinary ConvertToSqlBinary(object value)
        {
            if (value == DBNull.Value)
            {
                return(SqlBinary.Null);
            }
            Type        dataType    = value.GetType();
            StorageType storageType = DataStorageHelper.GetStorageType(dataType);

            if (storageType != StorageType.ByteArray)
            {
                if (storageType != StorageType.SqlBinary)
                {
                    throw InvalidExpressionException.SqlConvertFailed(dataType, typeof(SqlBinary));
                }
                return((SqlBinary)value);
            }
            return((byte[])value);
        }
Beispiel #8
0
        public static SqlString ConvertToSqlString(object value)
        {
            if (value == DBNull.Value || value == null)
            {
                return(SqlString.Null);
            }
            Type type = value.GetType();

            switch (DataStorageHelper.GetStorageType(type))
            {
            case StorageType.String:
                return((SqlString)((string)value));

            case StorageType.SqlString:
                return((SqlString)value);

            default:
                throw InvalidExpressionException.SqlConvertFailed(type, typeof(SqlString));
            }
        }
Beispiel #9
0
        public static SqlGuid ConvertToSqlGuid(object value)
        {
            if (value == DBNull.Value)
            {
                return(SqlGuid.Null);
            }
            Type type = value.GetType();

            switch (DataStorageHelper.GetStorageType(type))
            {
            case StorageType.Guid:
                return((SqlGuid)((Guid)value));

            case StorageType.SqlGuid:
                return((SqlGuid)value);

            default:
                throw InvalidExpressionException.SqlConvertFailed(type, typeof(SqlGuid));
            }
        }
Beispiel #10
0
        public static SqlBinary ConvertToSqlBinary(object value)
        {
            if (value == DBNull.Value)
            {
                return(SqlBinary.Null);
            }
            Type type = value.GetType();

            switch (DataStorageHelper.GetStorageType(type))
            {
            case StorageType.ByteArray:
                return((SqlBinary)((byte[])value));

            case StorageType.SqlBinary:
                return((SqlBinary)value);

            default:
                throw InvalidExpressionException.SqlConvertFailed(type, typeof(SqlBinary));
            }
        }
Beispiel #11
0
        public static object ChangeType2(object value, StorageType stype, Type type, IFormatProvider formatProvider)
        {
            switch (stype)
            {
            case StorageType.SqlBinary:
                return(ConvertToSqlBinary(value));

            case StorageType.SqlBoolean:
                return(ConvertToSqlBoolean(value));

            case StorageType.SqlByte:
                return(ConvertToSqlByte(value));

            case StorageType.SqlBytes:
                return(ConvertToSqlBytes(value));

            case StorageType.SqlChars:
                return(ConvertToSqlChars(value));

            case StorageType.SqlDateTime:
                return(ConvertToSqlDateTime(value));

            case StorageType.SqlDecimal:
                return(ConvertToSqlDecimal(value));

            case StorageType.SqlDouble:
                return(ConvertToSqlDouble(value));

            case StorageType.SqlGuid:
                return(ConvertToSqlGuid(value));

            case StorageType.SqlInt16:
                return(ConvertToSqlInt16(value));

            case StorageType.SqlInt32:
                return(ConvertToSqlInt32(value));

            case StorageType.SqlInt64:
                return(ConvertToSqlInt64(value));

            case StorageType.SqlMoney:
                return(ConvertToSqlMoney(value));

            case StorageType.SqlSingle:
                return(ConvertToSqlSingle(value));

            case StorageType.SqlString:
                return(ConvertToSqlString(value));

            default:
            {
                if ((DBNull.Value == value) || (value == null))
                {
                    return(DBNull.Value);
                }
                Type        dataType    = value.GetType();
                StorageType storageType = DataStorageHelper.GetStorageType(dataType);
                switch (storageType)
                {
                case StorageType.SqlBinary:
                case StorageType.SqlBoolean:
                case StorageType.SqlByte:
                case StorageType.SqlBytes:
                case StorageType.SqlChars:
                case StorageType.SqlDateTime:
                case StorageType.SqlDecimal:
                case StorageType.SqlDouble:
                case StorageType.SqlGuid:
                case StorageType.SqlInt16:
                case StorageType.SqlInt32:
                case StorageType.SqlInt64:
                case StorageType.SqlMoney:
                case StorageType.SqlSingle:
                case StorageType.SqlString:
                    throw InvalidExpressionException.SqlConvertFailed(dataType, type);
                }
                if (StorageType.String != stype)
                {
                    if (StorageType.TimeSpan != stype)
                    {
                        if (StorageType.String == storageType)
                        {
                            switch (stype)
                            {
                            case StorageType.Boolean:
                                if ("1" != ((string)value))
                                {
                                    if ("0" == ((string)value))
                                    {
                                        return(false);
                                    }
                                    goto Label_0547;
                                }
                                return(true);

                            case StorageType.Char:
                                return(((IConvertible)((string)value)).ToChar(formatProvider));

                            case StorageType.SByte:
                                return(((IConvertible)((string)value)).ToSByte(formatProvider));

                            case StorageType.Byte:
                                return(((IConvertible)((string)value)).ToByte(formatProvider));

                            case StorageType.Int16:
                                return(((IConvertible)((string)value)).ToInt16(formatProvider));

                            case StorageType.UInt16:
                                return(((IConvertible)((string)value)).ToUInt16(formatProvider));

                            case StorageType.Int32:
                                return(((IConvertible)((string)value)).ToInt32(formatProvider));

                            case StorageType.UInt32:
                                return(((IConvertible)((string)value)).ToUInt32(formatProvider));

                            case StorageType.Int64:
                                return(((IConvertible)((string)value)).ToInt64(formatProvider));

                            case StorageType.UInt64:
                                return(((IConvertible)((string)value)).ToUInt64(formatProvider));

                            case StorageType.Single:
                                return(((IConvertible)((string)value)).ToSingle(formatProvider));

                            case StorageType.Double:
                                return(((IConvertible)((string)value)).ToDouble(formatProvider));

                            case StorageType.Decimal:
                                return(((IConvertible)((string)value)).ToDecimal(formatProvider));

                            case StorageType.DateTime:
                                return(((IConvertible)((string)value)).ToDateTime(formatProvider));

                            case StorageType.TimeSpan:
                                return(XmlConvert.ToTimeSpan((string)value));

                            case StorageType.String:
                                return((string)value);

                            case StorageType.Guid:
                                return(XmlConvert.ToGuid((string)value));

                            case StorageType.ByteArray:
                            case StorageType.CharArray:
                            case StorageType.Type:
                                goto Label_0547;

                            case StorageType.Uri:
                                return(new Uri((string)value));
                            }
                        }
                        goto Label_0547;
                    }
                    switch (storageType)
                    {
                    case StorageType.Int32:
                        return(new TimeSpan((long)((int)value)));

                    case StorageType.UInt32:
                        goto Label_037A;

                    case StorageType.Int64:
                        return(new TimeSpan((long)value));

                    case StorageType.String:
                        return(XmlConvert.ToTimeSpan((string)value));
                    }
                    goto Label_037A;
                }
                switch (storageType)
                {
                case StorageType.Boolean:
                    return(((bool)value).ToString(formatProvider));

                case StorageType.Char:
                    return(((char)value).ToString(formatProvider));

                case StorageType.SByte:
                    return(((sbyte)value).ToString(formatProvider));

                case StorageType.Byte:
                    return(((byte)value).ToString(formatProvider));

                case StorageType.Int16:
                    return(((short)value).ToString(formatProvider));

                case StorageType.UInt16:
                    return(((ushort)value).ToString(formatProvider));

                case StorageType.Int32:
                    return(((int)value).ToString(formatProvider));

                case StorageType.UInt32:
                    return(((uint)value).ToString(formatProvider));

                case StorageType.Int64:
                    return(((long)value).ToString(formatProvider));

                case StorageType.UInt64:
                    return(((ulong)value).ToString(formatProvider));

                case StorageType.Single:
                    return(((float)value).ToString(formatProvider));

                case StorageType.Double:
                    return(((double)value).ToString(formatProvider));

                case StorageType.Decimal:
                    return(((decimal)value).ToString(formatProvider));

                case StorageType.DateTime:
                    return(((DateTime)value).ToString(formatProvider));

                case StorageType.TimeSpan:
                    return(XmlConvert.ToString((TimeSpan)value));

                case StorageType.String:
                    return((string)value);

                case StorageType.Guid:
                    return(XmlConvert.ToString((Guid)value));

                case StorageType.CharArray:
                    return(new string((char[])value));
                }
                break;
            }
            }
            IConvertible convertible = value as IConvertible;

            if (convertible != null)
            {
                return(convertible.ToString(formatProvider));
            }
            IFormattable formattable = value as IFormattable;

            if (formattable != null)
            {
                return(formattable.ToString(null, formatProvider));
            }
            return(value.ToString());

Label_037A:
            return((TimeSpan)value);

Label_0547:
            return(Convert.ChangeType(value, type, formatProvider));
        }