不正确的Guid秩序 [英] Incorrect Guid order

查看:138
本文介绍了不正确的Guid秩序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,我有一个GUID f5cc4100-f1b4-4af6-9e9e-224b0eb74166 ,我使用ADO .NET连接器其插入到MySQL数据库。



我能做到这一点有两种方式:



1)饲料字节数组到.NET连接器。



 查询字符串=插入测试(pk_id)值(@id); 
使用(VAR C =新的MySqlCommand(查询,康涅狄格州))
{
c.Parameters.AddWithValue(@ ID,新的GUID(f5cc4100-f1b4-4af6-9e9e-224b0eb74166 ).ToByteArray());
c.ExecuteNonQuery();
}



2)依靠MySQL的标准功能为十六进制转换为二进制字符串。

 查询字符串=插入测试(pk_id)值(UNHEX(@id)); 
使用(VAR C =新的MySqlCommand(查询,康涅狄格州))
{
c.Parameters.AddWithValue(@ ID,f5cc4100-f1b4-4af6-9e9e-224b0eb74166.Replace ( - ,);
c.ExecuteNonQuery();
}

我面临的问题是上述两种方式插入稍微不同的字符顺序的GUID相同



如果我选择回来插入的GUID是这样的:

 查询字符串=从测试选择PK_id; 
使用(VAR C =新的MySqlCommand(查询,康涅狄格州))
{
使用(VAR R = c.ExecuteReader())
{
,而(r.Read())
{
变种K =新的GUID ((字节[])R [0]);
}
}
}

我得到 f5cc4100-f1b4-4af6-9e9e-224b0eb74166 0041ccf5-b4f1-f64a-9e9e-224b0eb74166 回来,但如果我看是这样的:

 查询字符串=选择测试十六进制(PK_id); 
使用使用(VAR C =新的MySqlCommand(查询,康涅狄格州))
{
(VAR R = c.ExecuteReader())
{
,而(R。阅读())
{
变种K =新的GUID((串)R [0]);
}
}
}



我得到 0041ccf5-b4f1-f64a-9e9e-224b0eb74166 f5cc4100-f1b4-4af6-9e9e-224b0eb74166



总之,如果我插入GUID作为ByteArray对象,然后我需要阅读它们的方式与转换为字节数组,以获得正确的GUID。如果我插入GUID为 UNHEX 十六进制GUID字符串,那么我需要从数据库中使用它的等效 HEX 功能。




  1. 为什么写入和读出用上述两种方法有什么不同?


  2. 什么是两者之间处理的GUID的更合适的方式?



解决方案

由于原来的海报问我的意见(这只是链接)发布为答案,就到这里吧:



SO:在.NET



的Guid字节顺序

MSDN:的System.Guid .ToByteArray交换前4个字节



SO:C#:为什么不Guid.ToString(N)与从相同GUID的字节数组生成一个十六进制字符串?



好像它没有明确记载什么字节序的Guid 具备,转换和从当字节[]

$的不同组成部分b $ b

Ok I have a GUID f5cc4100-f1b4-4af6-9e9e-224b0eb74166 and I am inserting it to MySQL database using the ADO .NET Connector.

I can do it in two ways:

1) Feed byte array to .NET Connector

string query = "insert into test (pk_id) values (@id)";
using (var c = new MySqlCommand(query, conn))
{
    c.Parameters.AddWithValue("@id", new Guid("f5cc4100-f1b4-4af6-9e9e-224b0eb74166").ToByteArray());
    c.ExecuteNonQuery();
}

2) Rely on MySQL standard function to convert hexadecimal to binary string.

string query = "insert into test (pk_id) values (UNHEX(@id))";
using (var c = new MySqlCommand(query, conn))
{
    c.Parameters.AddWithValue("@id", "f5cc4100-f1b4-4af6-9e9e-224b0eb74166".Replace("-", "");
    c.ExecuteNonQuery();
}

The problem I face is the two above methods inserts the same guid in slightly different character order.

If I am selecting back the inserted guid like this:

string query = "select PK_id from test";
using (var c = new MySqlCommand(query, conn))
{
    using (var r = c.ExecuteReader())
    {
        while (r.Read())
        {
            var k = new Guid((byte[])r[0]);
        }
    }
}

I get f5cc4100-f1b4-4af6-9e9e-224b0eb74166 and 0041ccf5-b4f1-f64a-9e9e-224b0eb74166 back. But if I am reading like this:

string query = "select hex(PK_id) from test";
using (var c = new MySqlCommand(query, conn))
{
    using (var r = c.ExecuteReader())
    {
        while (r.Read())
        {
            var k = new Guid((string)r[0]);
        }
    }
}

I get 0041ccf5-b4f1-f64a-9e9e-224b0eb74166 and f5cc4100-f1b4-4af6-9e9e-224b0eb74166.

In short, if I am inserting the GUID as bytearrays, then I need to read them as such and cast as byte arrays to get the right GUID. And if I am inserting the GUID as UNHEX of the hexadecimal guid string, then I need to read from db using its equivalent HEX function.

  1. Why are writing and reading different with the above two methods?

  2. What would be the more appropriate way of handling GUIDs between the two?

解决方案

Since the Original Poster asked for my comments (which are just links) to be posted as an answer, here it comes:

SO: Guid Byte Order in .NET

MSDN: System.Guid .ToByteArray swapping first 4 bytes

SO: C#: Why isn't Guid.ToString("n") the same as a hex string generated from a byte array of the same guid?

It seems like it's not clearly documented what endianness the different components of the Guid possess, when converting to and from Byte[].

这篇关于不正确的Guid秩序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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