Possible duplicate:
How to store GUID in MySQL tables?
VARCHAR will return as string
BLOB will return as byte[]
but what type of MySQL column will return as System.Guid in C #
thanks.
Edited: I found the answer
thanks to DaveHogan, which gives me the key to ORM (object binding mapping)
I started looking for information about the conversion (mapping) of the data type created by Connector / NET. I am using MySQL Dot Net Connector (Connector / NET)
There are 3 different situations that Connector / NET performs.
Below I found:
1st situation:
Start with Connnector / NET 6.1.1, CHAR (36) will be automatically converted as System.GUID to C #, and BINARY (16) will be considered as a system. Byte [].
The second situation:
If you use the old Connector / NET, BINARY (16) will be automatically converted as System.GUID when the value is passed in C #, and CHAR (36) will be considered as System.String.
3rd situation:
If you are using the new Connector / NET (newer than 6.1.1), and if you want to use BINARY (16) AS System.GUID (this is not the case by default), you need to add the option Old Guids = true connection in the connection string.
Example:
server = localhost; user = root; password = qwerty; database = test; old guids = true;
CHAR (36) will be converted as System.String and BINARY (16) = System.GUID
More about connection options Old guides :
MySQL 5.6 Reference Guide: 21.2.6. Connector / Network Connection String Options
-------------------------------------------------- --------------------
Additional Information:
How to insert System.GUID into MySql database
-------------------------------------------------- --------------------
This is an example table that we are going to insert into System.GUID:
CREATE TABLE `testdata` (
`id` int (10) unsigned NOT NULL AUTO_INCREMENT,
`binary` binary (16) DEFAULT NULL,
`char` char (36) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE = InnoDB AUTO_INCREMENT = 1 DEFAULT CHARSET = utf8
Insert System.GUID into MySQL
string constr = "server = localhost; user = root; pwd = qwerty; database = test;"
using (MySqlConnection conn = new MySqlConnection (constr))
{
conn.Open ();
// Create a System.GUID
byte [] ba = new byte [16];
Random rd = new Random ();
rd.NextBytes (ba);
System.Guid guid = new Guid (ba);
// Prepare GUID values ββin SQL format
string guidForChar36 = guid.ToString ();
string hexstring = BitConverter.ToString (guid.ToByteArray ());
string guidForBinary16 = "0x" + hexstring.Replace ("-", string.Empty);
string sql = "insert into testdata (` binary`, `char`)"
+ "values ββ(" + guidForBinary16 + ","
+ "'" + guidForChar36 + "');";
MySqlCommand cmd = new MySqlCommand (sql, conn);
cmd.ExecuteNonQuery ();
conn.Close ();
}
source share