为什么我的 BLOB 字段仍然是 13B - 我做错了什么?
我想将数据存储在 MySQL BLOB 字段中。我使用以下代码:
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。您能给建议吗?谢谢!
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您正在使用字符串连接将字节数组转换为字符串。这不会给你你想要的数据。每行的 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.