Beispiel #1
0
 public override object ConvertDbValue(object value, Type expectedType)
 {
     if (value == null || value == DBNull.Value)
     {
         return(null);
     }
     if (expectedType.IsGenericType && expectedType.GetGenericTypeDefinition() == typeof(Nullable <>))
     {
         Type innerType = Nullable.GetUnderlyingType(expectedType);
         return(ConvertDbValue(value, innerType));
     }
     if (expectedType.IsEnum)
     {
         Type innerType = Enum.GetUnderlyingType(expectedType);
         return(Enum.ToObject(expectedType, ConvertDbValue(value, innerType)));
     }
     if (value is OracleDecimal)
     {
         OracleDecimal newValue = (OracleDecimal)value;
         if (expectedType == typeof(byte))
         {
             return(newValue.ToByte());
         }
         if (expectedType == typeof(Int16))
         {
             return(newValue.ToInt16());
         }
         if (expectedType == typeof(Int32))
         {
             return(newValue.ToInt32());
         }
         if (expectedType == typeof(Int64))
         {
             return(newValue.ToInt64());
         }
         if (expectedType == typeof(Single))
         {
             return(newValue.ToSingle());
         }
         if (expectedType == typeof(Double))
         {
             return(newValue.ToDouble());
         }
         if (expectedType == typeof(bool))
         {
             return(Convert.ToBoolean(newValue.ToInt32()));
         }
         throw new Exception("未实现Oracle.ManagedDataAccess.Types.OracleDecimal转化" + expectedType);
     }
     return(Convert.ChangeType(value, expectedType));
 }
Beispiel #2
0
        public static dynamic OracleDBTypeToNative(OracleParameter param)
        {
            switch (param.OracleDbTypeEx)
            {
            case OracleDbType.Array:
            case OracleDbType.BFile:
            case OracleDbType.BinaryDouble:
            case OracleDbType.BinaryFloat:
            case OracleDbType.Ref:
            case OracleDbType.RefCursor:
            case OracleDbType.XmlType:
                return(param.Value);

            case OracleDbType.LongRaw:
            case OracleDbType.Raw:
            case OracleDbType.Blob:
                if (param.Value == null)
                {
                    return(default(byte[]));
                }
                else
                {
                    /*TODO: this is an extremely naive implementation, and will eat RAM if large BLOBS are used.
                     *      Currently, I'm limiting at 10MB of data (defined in a const inside _Common.cs),
                     *      and raising an error if this limit is exceeded. */
                    byte[] BlobBytes;

                    OracleBlob Blob = (OracleBlob)param.Value;
                    if (Blob.Length < Max_Blob_Size)
                    {
                        BlobBytes = new byte[Blob.Length];
                        Blob.Read(BlobBytes, 0, (int)Blob.Length);
                        return(BlobBytes);
                    }
                    else
                    {
                        throw new NotSupportedException("This function will return a maximum of " + Max_Blob_Size + " bytes to avoid excessive RAM consumption.");
                    }
                }

            //this case will probably never work, so I may as well ignore it

            /*case OracleDbType.Byte:
             *  if(param.Value == null)
             *  {
             *      return default(byte);
             *  }
             *  else
             *  {
             *      return (byte)param.Value;
             *  }*/
            case OracleDbType.Char:
            case OracleDbType.NChar:
            case OracleDbType.NVarchar2:
            case OracleDbType.Varchar2:
                OracleString paramValueString = (OracleString)param.Value;
                if (paramValueString == null || paramValueString.IsNull)
                {
                    return(string.Empty);
                }
                else
                {
                    return(paramValueString.Value);
                }

            case OracleDbType.Clob:
            case OracleDbType.NClob:
                if (param.Value == null)
                {
                    return(default(string));
                }
                else
                {
                    return(((OracleClob)param.Value).Value);
                }

            case OracleDbType.Date:
                OracleDate paramValueDate = (OracleDate)param.Value;
                if (paramValueDate == null || paramValueDate.IsNull)
                {
                    return(default(DateTime));
                }
                else
                {
                    return(paramValueDate.Value);
                }

            case OracleDbType.IntervalDS:
                if (param.Value == null)
                {
                    return(default(TimeSpan));
                }
                else
                {
                    return(((OracleIntervalDS)param.Value).Value);
                }

            case OracleDbType.IntervalYM:
                if (param.Value == null)
                {
                    return(default(TimeSpan));
                }
                else
                {
                    return(((OracleIntervalYM)param.Value).Value);
                }

            case OracleDbType.TimeStamp:
                if (param.Value == null)
                {
                    return(default(DateTime));
                }
                else
                {
                    return(((OracleTimeStamp)param.Value).Value);
                }

            case OracleDbType.TimeStampLTZ:
                if (param.Value == null)
                {
                    return(default(DateTime));
                }
                else
                {
                    return(((OracleTimeStampLTZ)param.Value).Value);
                }

            case OracleDbType.TimeStampTZ:
                if (param.Value == null)
                {
                    return(default(DateTime));
                }
                else
                {
                    return(((OracleTimeStampTZ)param.Value).Value);
                }

            case OracleDbType.Int16:
            case OracleDbType.Int32:
                OracleDecimal paramValueInt32 = (OracleDecimal)param.Value;
                if (paramValueInt32 == null || paramValueInt32.IsNull)
                {
                    return(default(int));
                }
                else
                {
                    return(paramValueInt32.ToInt32());
                }

            case OracleDbType.Int64:
                OracleDecimal paramValueInt64 = (OracleDecimal)param.Value;
                if (paramValueInt64 == null || paramValueInt64.IsNull)
                {
                    return(default(Int64));
                }
                else
                {
                    return(paramValueInt64.ToInt64());
                }

            case OracleDbType.Decimal:
                OracleDecimal paramValueDecimal = (OracleDecimal)param.Value;
                if (paramValueDecimal == null || paramValueDecimal.IsNull)
                {
                    return(default(decimal));
                }
                else
                {
                    return(paramValueDecimal.Value);
                }

            case OracleDbType.Double:
            case OracleDbType.Single:     //we don't care internally about single.
                if (param.Value == null)
                {
                    return(default(double));
                }
                else
                {
                    return(((OracleDecimal)param.Value).ToDouble());
                }

            default:
                throw new NotImplementedException("Type not handled yet");
            }
        }