为什么我的 BLOB 字段仍然是 13B - 我做错了什么?

发布于 2024-08-12 21:12:21 字数 590 浏览 1 评论 0原文

我想将数据存储在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

南风起 2024-08-19 21:12:21

您正在使用字符串连接将字节数组转换为字符串。这不会给你你想要的数据。每行的 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文