简体   繁体   English

C#:与OracleDbType等效的Oracle数据类型

[英]C#: Oracle Data Type Equivalence with OracleDbType


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. 我正在C#中创建一个应用程序,该应用程序使用Oracle.DataAccess.Client(11g)对具有存储过程的Oracle数据库进行某些操作。 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. 我知道有一个包含Oracle数据类型的特定枚举(OracleDbType),但是我不确定用于某些类型的枚举。

Questions: 问题:

  • What is the equivalent Oracle PL/SQL data type for each enumerated type in the OracleDbType enumeration ? OracleDbType枚举中每个枚举类型的等效Oracle PL / SQL数据类型是什么?

  • There are three types of integer 整数有三种类型
    (Int16, Int32, Int64) in the OracleDbType... how to know which one to use or are they all OracleDbType中的(Int16,Int32,Int64)...如何知道要使用哪个还是全部
    suppose to work? 想工作吗


Here's a method to convert C# types to the most common OracleDbTypes 这是将C#类型转换为最常见的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 . 另外,对于非常大的字符数据值,您可能需要使用OracleDbType.Clob

The values of the OracleDbType Enumeration are defined in the documentation. OracleDbType枚举的值在文档中定义。 Read the ODP for .NET Developer's Guide . 阅读ODP for .NET开发人员指南

With regards to choosing between Int16, Int32 and Int64, they are all supposed to work. 关于在Int16,Int32和Int64之间进行选择,它们都应该起作用。 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. 选择一个与您的.Net变量的预期大小相匹配的变量:Int16表示-32768和32767之间的值,Int32表示-2147483648和2147483647之间的值,以及Int64表示更大的值。 There appear to be some funnies relating to converting Ints and PL/SQL data types. 似乎有一些与转换Ints和PL / SQL数据类型有关的有趣的事情。 Check this blog post by Mark Williams . 查看Mark Williams的这篇博客文章

Check APC's links out, they are what you are looking for : the mapping is quite straightforward according to the name of the enumeration. 检查一下APC的链接 ,它们就是您要寻找的:根据枚举的名称,映射非常简单。

But as you began to notice, there is something tricky about integers. 但是,正如您开始注意到的那样,整数有些棘手。 Here is my mapping : 这是我的映射:

  • Int16 : NUMBER(5) . Int16NUMBER(5)
  • Int32 : NUMBER(10) . Int32NUMBER(10)
  • Int64 : NUMBER(19) . Int64NUMBER(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(38)列上调用GetInt64 ,即使该值在正确的范围内,您也会收到异常。

NUMBER(1,0) => Boolean NUMBER(1,0)=>布尔值

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

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

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

NUMBER(19,0) => long.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. 如果在oracle中使用Number,则十进制是必经之路。

As APC pointed: https://docs.oracle.com/cd/B19306_01/win.102/b14307/OracleDbTypeEnumerationType.htm 正如APC所指出的那样: https : //docs.oracle.com/cd/B19306_01/win.102/b14307/OracleDbTypeEnumerationType.htm

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM