如何将文件 (PDF) 插入 varbinary SQL Server 列并稍后检索它?

发布于 2024-07-11 23:51:03 字数 216 浏览 2 评论 0原文

我希望获取报表运行的结果(来自 Crystal Reports 的 PDF 文件),对其进行序列化,将其粘贴到 varbinary 字段中,然后能够对其进行反序列化并将其呈现给用户。

现在我只能使用普通的旧式 ADO .NET(SqlClient、SqlCommand 等)。

这有什么陷阱吗? 鉴于我知道当前保存 PDF 文件的本地计算机上的路径,完成此操作的基本语法是什么?

I'm looking to take the results of a report run (a PDF file from Crystal Reports), serialize it, stick it into a varbinary field, and then later be able to deserialize it and present it back to the user.

For now I have to just plain old ADO .NET (SqlClient, SqlCommand, etc.)

Are there any pitfalls to this? What is the basic syntax to accomplish this given the fact that I would know the path on the local machine where the PDF file was currently saved?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

來不及說愛妳 2024-07-18 23:51:03

编辑:刚刚在MSDN文档 图像将被删除。 最好使用 VARBINARY(MAX)。 (嘿我不知道)。

我的旧答案:VARBINARY 的大小限制为 8 KB。 可能太小而无法存储 PDF。 您最好使用 IMAGE 数据类型。

使用参数通过 SqlCommand 插入/选择数据,并且您可以像任何普通字段一样使用纯 SQL。

传递到图像字段的数据是一个字节数组。

编辑 2:由于您使用的是 C#,因此您应该使用 DataReader 检索数据。 要创建 DataReader,请使用 SqlCommand.ExecuteReader 上的 SequentialAccess 标志,以防止数据过早读入内存。

EDIT: Just seen in the MSDN documentation that Image is going to be removed. Better use use VARBINARY(MAX). (Hey I did not know that).

MY OLD ANSWER: VARBINARY has a limit of 8 KB. Can be too small to store PDF's. You'd better use the IMAGE datatype.

Use parameters to insert/select your data via the SqlCommand, and you can use just plain SQL like any ordinary field.

The data you pass into the image field is an array of bytes.

Edit 2: Since you are using C#, you should retrieve the data using a DataReader. To create the DataReader use the SequentialAccess flag on the SqlCommand.ExecuteReader, to prevent the data being read into memory too early.

天气好吗我好吗 2024-07-18 23:51:03

我真的不会在 IDbCommand 中使用 byte[] 参数。 如果您的 PDF 有 100 Mb 大怎么办? 据我所知,这会在将结果发送到请求浏览器之前从您的数据库中获取所有 100 Mb(需要服务器上的内存量)。 现在考虑一下每秒获得大约这个大小的不同 PDF 的多个并发请求...这不是非常可扩展的。

有关可能的解决方案,请查看“Blob + Stream = BlobStream”,几年前我为荷兰的 .NET 杂志撰写的一篇文章。 此方法使用 SQL Server 命令从 Stream 访问 BLOB 的部分内容代码>派生类。

该文章是荷兰语的,但 与之配套的示例代码应该足以帮助您入门。 另外,Peter De Jonghe 似乎写了一篇关于 CodeProject 的文章,该文章扩展我的文章对此进行了一些概括,将其推广到 SQL Server 中的不仅仅是 image 列。 本文是英文的。

我希望这可以帮助您解决您的问题。

I really wouldn't use a byte[] parameter to an IDbCommand at all. What if your PDF is 100 Mb big? As far as I know, this would get all those 100 Mb from your database (needing that amount of memory on your server) before sending the result to the requesting browser. Now think about getting a number of concurrent requests for different PDFs of roughly this size... per second... That is not very scalable.

For a possible solution, please check out "Blob + Stream = BlobStream", an article I wrote for the .NET Magazine here in the Netherlands a couple of years ago. This method uses SQL Server commands to access parts of a BLOB from a Stream derived class.

The article is in Dutch, but the example code that goes with it should be enough to get you started. Also, Peter De Jonghe seems to have written an article on CodeProject that expands on my article a bit, generalizing it to more than just image columns in SQL Server. This article is in English.

I hope this helps you with your problem.

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