Why my BLOB field is still 13B - what I am doing wrong?

回眸只為那壹抹淺笑 提交于 2019-12-24 08:03:19

问题


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:


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.



来源:https://stackoverflow.com/questions/1790348/why-my-blob-field-is-still-13b-what-i-am-doing-wrong

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!