views:

1401

answers:

3

I like to save different c# data types in a Oracle database (int, decimal, double, string, Guid, …). Does anyone have a table showing what oracle data types to use?

I have found tables showing what c# data types to use for different oracle data types but not the other way around.

+4  A: 

See here

Preet Sangha
Thanks for the quick response. But it was not what I ment.Your list is good when you have a database and wish to known what c# datatype to use. I have a c# application and wish to known what Oracle datatype to use. I like something like this:C# datatype -> Oracle datatypeint -> NUMBER(4)string -> varchar2…
Rolf
@Rolf isn't this just a reverse lookup on the same table? The mapping doesn't change, just begin in the last column and look left??
Dave Anderson
Dave: Well yes but it is not unique that way. Let’s take System.Decimal what is the best implementation of this in Oracle? BINARY_DOUBLE, BINARY_FLOAT, NUMBER? I like a unique table.
Rolf
There is no unique table. Some C# datatypes have no mapping to oracle (bool). You can map a bool to number but this is not unique as you expect. A C# string can have any length. The best match would be a NCLOB. But a LOB column cannot be indexed. This leads to bad query performance.
Christian13467
Christian: Good point. But still it would be nice with a list to help choosing an oracle data type. So a list with recommendations for data types would be nice.
Rolf
+1  A: 

I asked a similar question regarding SQL Server data types and got this answer which may have some relevance for you as well.

grenade
Yes the problem is the same. But the solution does not contain an Oracle specific conversion of data types.
Rolf
+1  A: 

I'm not sure if this helps or not, but this was taken from the ODP.NET assembly using .NET Reflector:

internal static void InsertTableEntries()
{
    s_table.Add(typeof(byte), OracleDbType.Byte);
    s_table.Add(typeof(byte[]), OracleDbType.Raw);
    s_table.Add(typeof(char), OracleDbType.Varchar2);
    s_table.Add(typeof(char[]), OracleDbType.Varchar2);
    s_table.Add(typeof(DateTime), OracleDbType.TimeStamp);
    s_table.Add(typeof(short), OracleDbType.Int16);
    s_table.Add(typeof(int), OracleDbType.Int32);
    s_table.Add(typeof(long), OracleDbType.Int64);
    s_table.Add(typeof(float), OracleDbType.Single);
    s_table.Add(typeof(double), OracleDbType.Double);
    s_table.Add(typeof(decimal), OracleDbType.Decimal);
    s_table.Add(typeof(string), OracleDbType.Varchar2);
    s_table.Add(typeof(TimeSpan), OracleDbType.IntervalDS);
    s_table.Add(typeof(OracleBFile), OracleDbType.BFile);
    s_table.Add(typeof(OracleBinary), OracleDbType.Raw);
    s_table.Add(typeof(OracleBlob), OracleDbType.Blob);
    s_table.Add(typeof(OracleClob), OracleDbType.Clob);
    s_table.Add(typeof(OracleDate), OracleDbType.Date);
    s_table.Add(typeof(OracleDecimal), OracleDbType.Decimal);
    s_table.Add(typeof(OracleIntervalDS), OracleDbType.IntervalDS);
    s_table.Add(typeof(OracleIntervalYM), OracleDbType.IntervalYM);
    s_table.Add(typeof(OracleRefCursor), OracleDbType.RefCursor);
    s_table.Add(typeof(OracleString), OracleDbType.Varchar2);
    s_table.Add(typeof(OracleTimeStamp), OracleDbType.TimeStamp);
    s_table.Add(typeof(OracleTimeStampLTZ), OracleDbType.TimeStampLTZ);
    s_table.Add(typeof(OracleTimeStampTZ), OracleDbType.TimeStampTZ);
    s_table.Add(typeof(OracleXmlType), OracleDbType.XmlType);
    s_table.Add(typeof(OracleRef), OracleDbType.Ref);
}

Internally it looks like ODP.NET uses this (and a few other maps) to determine data types. Also, to handle the NUMBER data type:

internal static OracleDbType ConvertNumberToOraDbType(int precision, int scale)
{
    OracleDbType @decimal = OracleDbType.Decimal;
    if ((scale <= 0) && ((precision - scale) < 5))
    {
        return OracleDbType.Int16;
    }
    if ((scale <= 0) && ((precision - scale) < 10))
    {
        return OracleDbType.Int32;
    }
    if ((scale <= 0) && ((precision - scale) < 0x13))
    {
        return OracleDbType.Int64;
    }
    if ((precision < 8) && (((scale <= 0) && ((precision - scale) <= 0x26)) || ((scale > 0) && (scale <= 0x2c))))
    {
        return OracleDbType.Single;
    }
    if (precision < 0x10)
    {
        @decimal = OracleDbType.Double;
    }
    return @decimal;
}
rally25rs
Thank you for the answer! This gives me some clues to what to choose.
Rolf