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

发布于 2024-08-08 16:28:55 字数 381 浏览 8 评论 0原文


情况:

我正在 C# 中创建一个应用程序,该应用程序使用 Oracle.DataAccess.Client (11g) 通过存储过程对 Oracle 数据库执行某些操作。我知道有一个特定的枚举 (OracleDbType) 包含 Oracle 数据类型,但我不确定对于某些类型使用哪一个。

问题:

  • 等效的 Oracle PL/SQL 数据是什么 type 中的每个枚举类型 OracleDbType 枚举

  • 整数分为三种类型
    OracleDbType 中的(Int16、Int32、Int64)...如何知道 使用哪一个或全部
    应该工作吗?



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?


如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

逆光飞翔i 2024-08-15 16:28:55

下面是将 C# 类型转换为最常见的 OracleDbType 的方法。

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;
}

此外,对于非常大的字符数据值,您可能需要使用 OracleDbType.Clob

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.

神妖 2024-08-15 16:28:55

OracleDbType 枚举的值在文档中定义。阅读ODP for .NET 开发人员指南

至于在 Int16、Int32 和 Int64 之间进行选择,它们都应该有效。选择与 .Net 变量的预期大小相匹配的变量:Int16 表示 -32768 到 32767 之间的值,Int32 表示 -2147483648 到 2147483647 之间的值,Int64 表示任何更大的值。似乎有一些与转换 Int 和 PL/SQL 数据类型有关的有趣事情。检查 Mark Williams 的这篇博文


2021 年的尾声问候

。这篇文章刚刚被投票,所以想必探索者仍在寻找它并发现它有用。但请注意,已有十多年历史。因此,它提供的建议可能与最新版本的 ODP 无关,尤其是关于异常行为的建议。请不要在没有亲自验证的情况下遵循建议。 (这是一般建议,适用于您在 Internet 上阅读的任何内容,而不仅仅是软件开发!)

这里是 Oracle 21c ODP 文档的相关链接

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.

记忆之渊 2024-08-15 16:28:55

检查APC的链接,它们就是你的寻找:根据枚举的名称,映射非常简单。

但正如您开始注意到的那样,整数有一些棘手的地方。这是我的映射:

  • Int16NUMBER(5)
  • Int32NUMBER(10)
  • Int64NUMBER(19)

问题是,如果您调用 GetInt64NUMBER(38) 列上,即使该值在正确的范围内,您也会收到异常...

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...

万劫不复 2024-08-15 16:28:55

数字(1,0)=>布尔

值(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

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

星星的轨迹 2024-08-15 16:28:55

对于那些想知道浮点数等价物的人:

Decimal     Oracle NUMBER type
Double      8-byte FLOAT type

如果您在 Oracle 中使用 Number,则可以选择 Decimal。

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文