转换字节数组从Oracle的RAW到的System.Guid? [英] Convert byte array from Oracle RAW to System.Guid?

查看:701
本文介绍了转换字节数组从Oracle的RAW到的System.Guid?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

我不能更改数据库架构或查询(因为它是重复使用的SQL Server)。我需要code将字节数组转换从Oracle到正确的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.

推荐答案

事实证明,这个问题是您在 Guid.ToByteArray(),并没有得到字节顺序甲骨文本身。如果你的GUID 11223344-5566-7788-9900-AABBCCDDEEFF ,并呼吁的toByteArray()就可以了,你会得到 44332211665588779900AABBCCDDEEFF 。如果再传递字节数组回构造的Guid,你原来的GUID。我的错误是试图查询,而不是的toByteArray()调用的结果Oracle数据库由原始的GUID格式(删除破折号)。

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.

我仍然不知道为什么字节有序的方式,但它显然无关与Oracle。

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

这篇关于转换字节数组从Oracle的RAW到的System.Guid?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆