开发者

C#: Oracle Data Type Equivalence with OracleDbType

开发者 https://www.devze.com 2022-12-08 15:08 出处:网络
Situation: I am cr开发者_如何学Goeating 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 certai

Situation:

I am cr开发者_如何学Goeating 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?



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.


The values of the OracleDbType Enumeration are defined in the documentation. Read the ODP for .NET Developer's Guide.

With regards to choosing between Int16, Int32 and Int64, they are all supposed to work. Choose the one which matches the expected size of your .Net variable: Int16 for values between -32768 and 32767, Int32 for values between -2147483648 and 2147483647, and Int64 for anything larger. There appear to be some funnies relating to converting Ints and PL/SQL data types. Check this blog post by Mark Williams.


Coda

Greetings from 2021. This post has just been upvoted, so presumably Seekers are still finding it and finding it useful. But please note that it is more than ten years old. Consequently the advice it offers may not be relevant for more recent versions of ODP, particularly regarding unusual behaviour. Please don't follow recommendations without verifying them for yourself. (This is general advice which holds for anything you read on the Internet, and not just about software development!)

Here is the pertinent link for Oracle 21c ODP documentation.


Check APC's links out, they are what you are looking for : the mapping is quite straightforward according to the name of the enumeration.

But as you began to notice, there is something tricky about integers. Here is my mapping :

  • Int16 : NUMBER(5).
  • Int32 : NUMBER(10).
  • Int64 : NUMBER(19).

The thing is that if you call GetInt64 on a NUMBER(38) column, you will get an exception even if the value is in the correct range...


NUMBER(1,0) => Boolean

NUMBER(5,0) => Int16.MaxValue == 32767

NUMBER(10,0) => Int32.MaxValue == 2,147,483,647

NUMBER(19,0) => Int64.MaxValue == 9,223,372,036,854,775,807

NUMBER(19,0) => long.MaxValue == 9,223,372,036,854,775,807


For those who wants to know the equivalent of de floating points:

Decimal     Oracle NUMBER type
Double      8-byte FLOAT type

Decimal is the way to go if you used Number in oracle.

As APC pointed: https://docs.oracle.com/cd/B19306_01/win.102/b14307/OracleDbTypeEnumerationType.htm

0

精彩评论

暂无评论...
验证码 换一张
取 消