1

I would like to store the data in the MySQL BLOB field. I am using the following code:

int length = (int)fs.Length;
                        buffer = new byte[length];
                        int count;
                        int sum = 0;
                        while ((count = fs.Read(buffer, sum, length - sum)) > 0)
                            sum += count;

                        prikaz.CommandText = "INSERT INTO attachments (ReferenceID,Filename,File) VALUES ('" + referenceID + "','test','" + buffer + "')";
                        prikaz.ExecuteNonQuery();

But in the database, the BLOB field has always 13B. Could you please advice? Thanks!

Petr
  • 7,787
  • 14
  • 44
  • 53

1 Answers1

1

You're converting the byte array to a string using string concatenation. That won't give you the data you want. The data in your blob field will be the same for every row: the string "System.Byte[]" in ASCII, I suspect.

Always use a parameterized command for this sort of thing, so that you don't need to worry about conversions and escaping.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194