C#: Oracle Data Type Equivalence with OracleDbType

Partial picture Partial · Oct 17, 2009 · Viewed 78k times · Source

Situation:

I am creating an app in C# that uses Oracle.DataAccess.Client (11g) to do certain operations on a Oracle database with stored procedures. I am aware that there is a certain enum (OracleDbType) that contains the Oracle data types, but I am not sure which one to use for certain types.

Questions:

  • What is the equivalent Oracle PL/SQL data type for each enumerated type in the OracleDbType enumeration?

  • There are three types of integer
    (Int16, Int32, Int64) in the OracleDbType... how to know which one to use or are they all
    suppose to work?


Answer

Charles Bretana picture Charles Bretana · Oct 17, 2009

Here's a method to convert C# types to the most common OracleDbTypes

private static OracleDbType GetOracleDbType(object o) 
{
  if (o is string) return OracleDbType.Varchar2;
  if (o is DateTime) return OracleDbType.Date;
  if (o is Int64) return OracleDbType.Int64;
  if (o is Int32) return OracleDbType.Int32;
  if (o is Int16) return OracleDbType.Int16;
  if (o is sbyte) return OracleDbType.Byte;
  if (o is byte) return OracleDbType.Int16;    -- <== unverified
  if (o is decimal) return OracleDbType.Decimal;
  if (o is float) return OracleDbType.Single;
  if (o is double) return OracleDbType.Double;
  if (o is byte[]) return OracleDbType.Blob;

  return OracleDbType.Varchar2;
}

Also, for very large character data values, you may want to use OracleDbType.Clob.