title: What column type of MySQL will return as System.Guid in C#
body: <blockquote>
    <p><strong>Possible Duplicate:</strong><br>
    <a href="https://stackoverflow.com/questions/412341/how-should-i-store-guid-in-mysql-tables">How should I store GUID in MySQL tables?</a>  </p>
  </blockquote>
  
  
  
  <p><br />
  <strong>VARCHAR</strong> will return as <code>string</code><br />
  <strong>BLOB</strong> will return as <code>byte[]</code><br />
  but, what column type of MySQL will return as <strong>System.Guid</strong> in C#<br />
  thanks.<br /><br />
  <strong>Edited: I have found the answer</strong><br /><br />
  thanks to DaveHogan that gives me the clue about ORM (Object Relation Mapping)<br />
  I started to look for information about the datatype conversion(mapping) made by Connector/NET.
  I'm using MySQL Dot Net Connector (Connector/NET)<br />
  there are different 3 situations that Connector/NET performs.<br />
  <br />
  Below are what I have found:<br/><br />
  <strong>1st Situation:</strong><br />
  Start from Connnector/NET 6.1.1, <strong>CHAR(36)</strong> will be automatically converted as System.GUID in C# and <strong>BINARY(16)</strong> will be treated as System.Byte[].<br />
  <br />
  <strong>2nd Situation:</strong><br />
  If you use older Connector/NET, <strong>BINARY(16)</strong> will be automatically converted as System.GUID when the value is passed into C# and <strong>CHAR(36)</strong> will be treated as System.String.<br />
  <br />
  <strong>3rd Situation:</strong><br />
  If you use newer Connector/NET (newer than 6.1.1), and if you want to use <strong>BINARY(16)</strong> AS System.GUID (be default it is not), you have to add a connection option <strong>Old Guids=true</strong> in the Connection String.<br />Example:<br /></p>
  
  <pre>server=localhost;user=root;password=qwerty;database=test;old guids=true;</pre>
  
  <p>by this, <strong>CHAR(36)</strong> will be converted as System.String and <strong>BINARY(16)</strong> = System.GUID
  <br /><br />
  Read more about connection options of <strong>Old Guids</strong> at:<br /><a href="http://dev.mysql.com/doc/refman/5.6/en/connector-net-connection-options.html" rel="nofollow noreferrer">MySQL 5.6 Reference Manual: 21.2.6. Connector/Net Connection String Options Reference</a><br />
  <br />
  ----------------------------------------------------------------------<br />
  Extra Info:<br />
  <strong>How to Insert System.GUID into MySql Database</strong><br />
  ----------------------------------------------------------------------<br />
  This is a sample table, that we are going to insert the System.GUID:<br /></p>
  
  <pre>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
  </pre>
  
  <p>Insert System.GUID into MySQL</p>
  
  <pre>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();
  }</pre>