0

There is a MySQL query parameter of type BINARY. Binary values in MySQL can be represented like x'1f7a8e'. However, when adding a parameter of type MySqlDbType.Binary to a MySqlCommand the query is formed with the value represented as _binary '1f7a8e' which causes the query to not find the row. Below you can see a code example of how the MySQL API is being used.

var baz = "1f7a8e";
var cmd = new MySqlCommand("SELECT foo FROM bar WHERE baz=@baz");
cmd.Parameters.Add("@baz", MySqlDbType.Binary).Value = baz;

// Execute query and read result

What would be the correct way of using a binary value as a parameter?

Philipp
  • 876
  • 10
  • 23

2 Answers2

0

I was trying to store a Guid as a BINARY(16).

First I tried guid.ToByteArray() which did not work. Then I tried guid.ToString().Replace("-", "") which also failed.

Thanks to @daniel-a-white I then tried guid.ToString().Replace("-", "") and then converted the resulting hexadecimal number into a byte array (as described here) and that worked.

Philipp
  • 876
  • 10
  • 23
0

You can store GUID as Binary (16)

UNHEX(REPLACE(@baz, "-","")) 

This will return binary(16).

And to SELECT

SELECT * FROM mytable WHERE HEX(baz) = @baz
Allen King
  • 2,372
  • 4
  • 34
  • 52