将字节数组从 Oracle RAW 转换为 System.Guid?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2667740/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 20:26:25  来源:igfitidea点击:

Convert byte array from Oracle RAW to System.Guid?

c#.netoracleado.netguid

提问by Cory McCarty

My app interacts with both Oracle and SQL Server databases using a custom data access layer written in ADO.NET using DataReaders. Right now I'm having a problem with the conversion between GUIDs (which we use for primary keys) and the Oracle RAW datatype. Inserts into oracle are fine (I just use the ToByteArray() method on System.Guid). The problem is converting back to System.Guid when I load records from the database. Currently, I'm using the byte array I get from ADO.NET to pass into the constructor for System.Guid. This appears to be working, but the Guids that appear in the database do not correspond to the Guids I'm generating in this manner.

我的应用程序使用在 ADO.NET 中使用 DataReaders 编写的自定义数据访问层与 Oracle 和 SQL Server 数据库交互。现在我遇到了 GUID(我们用于主键)和 Oracle RAW 数据类型之间的转换问题。插入 oracle 很好(我只使用 System.Guid 上的 ToByteArray() 方法)。当我从数据库加载记录时,问题是转换回 System.Guid。目前,我使用从 ADO.NET 获得的字节数组传递给 System.Guid 的构造函数。这似乎有效,但出现在数据库中的 Guid 与我以这种方式生成的 Guid 不对应。

I can't change the database schema or the query (since it's reused for SQL Server). I need code to convert the byte array from Oracle into the correct Guid.

我无法更改数据库架构或查询(因为它已重用于 SQL Server)。我需要代码将字节数组从 Oracle 转换为正确的 Guid。

采纳答案by Cory McCarty

It turns out that the issue was the byte order you get in Guid.ToByteArray()and not Oracle itself. If you take the Guid "11223344-5566-7788-9900-aabbccddeeff" and call ToByteArray()on it, you get "44332211665588779900AABBCCDDEEFF". If you then pass that byte array back into the constructor for Guid, you get the original Guid. My mistake was trying to query the Oracle database by the original Guid format (with the dashes removed) instead of the result of the ToByteArray()call.

事实证明,问题在于您输入的字节顺序,Guid.ToByteArray()而不是 Oracle 本身。如果你拿着 Guid " 11223344-5566-7788-9900-aabbccddeeff" 并调用ToByteArray()它,你会得到 " 44332211665588779900AABBCCDDEEFF"。如果您随后将该字节数组传递回 Guid 的构造函数,您将获得原始 Guid。我的错误是尝试通过原始 Guid 格式(删除破折号)而不是ToByteArray()调用结果来查询 Oracle 数据库。

I still have no idea why the bytes are ordered that way, but it apparently has nothing to do with Oracle.

我仍然不知道为什么字节是这样排序的,但它显然与 Oracle 无关。

回答by samneric

I just had this same issue when storing and reading Guids from Oracle.

我在从 Oracle 存储和读取 Guid 时遇到了同样的问题。

If your app needs to store and read Guids from Oracle, use the FlipEndian function from this thread:

如果您的应用程序需要从 Oracle 存储和读取 Guid,请使用此线程中的 FlipEndian 函数:

.NET Native GUID conversion

.NET 本机 GUID 转换

Byte[] rawBytesFromOracle;
Guid dotNetGuid = new Guid(rawBytesFromOracle).FlipEndian();

The flip is only required when reading back from Oracle.

只有从 Oracle 读回时才需要翻转。

When writing to Oracle use Guid.ToByteArray() as normal.

写入 Oracle 时,照常使用 Guid.ToByteArray()。

I spent TOO much time trying to get this simple task accomplished.

我花了太多时间试图完成这个简单的任务。

Steve

史蒂夫

回答by Jon Skeet

I have vague recollections that the GUIDs from Oracle are effectively reversed compared with the order .NET expects.

我模糊地记得,与 .NET 预期的顺序相比,来自 Oracle 的 GUID 被有效地颠倒了。

Try reversing the array before calling the Guidconstructor.

在调用Guid构造函数之前尝试反转数组。

It may not be quiteas simple as reversing, however - you may need to do more detailed swapping. I suggest you create a GUID where each byte is easy to identify (use 0x01, 0x23, 0x45 etc) and work from there.

它可能不是简单但是作为倒车, -你可能需要做更详细的交换。我建议您创建一个 GUID,其中每个字节都易于识别(使用 0x01、0x23、0x45 等)并从那里开始工作。