为什么我的BLOB字段仍然是13B-我做错了什么? [英] Why my BLOB field is still 13B - what I am doing wrong?
问题描述
我想将数据存储在MySQL BLOB字段中.我正在使用以下代码:
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();
但是在数据库中,BLOB字段始终为13B.能否请您指教?谢谢!
But in the database, the BLOB field has always 13B. Could you please advice? Thanks!
推荐答案
您正在使用字符串连接将字节数组转换为字符串.那不会给您想要的数据.每行的blob字段中的数据都是相同的:我怀疑是ASCII字符串"System.Byte []".
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.
这篇关于为什么我的BLOB字段仍然是13B-我做错了什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!