tags:

views:

53

answers:

1

Hi, 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!

+1  A: 

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