Good. I have a GUID f5cc4100-f1b4-4af6-9e9e-224b0eb74166and I insert it into the MySQL database using the ADO.NET Connector.
I can do this in two ways:
1) Byte array array in .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 the standard MySQL 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 am facing is that the two above methods insert the same guid into a slightly different character order.
If I select the back-inserted guid as follows:
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'm getting f5cc4100-f1b4-4af6-9e9e-224b0eb74166and 0041ccf5-b4f1-f64a-9e9e-224b0eb74166back. But if I read 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-224b0eb74166and f5cc4100-f1b4-4af6-9e9e-224b0eb74166.
, GUID bytearrays, , GUID. GUID UNHEX guid, db, HEX.