简体   繁体   English

如何将文件(PDF)插入到varbinary SQL Server列中,然后检索它?

[英]How do you insert a file (PDF) into a varbinary SQL Server column and later retrieve it?

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. 我希望获取运行报告的结果(Crystal Reports的PDF文件),序列化它,将其粘贴到varbinary字段中,然后再将其反序列化并将其呈现给用户。

For now I have to just plain old ADO .NET (SqlClient, SqlCommand, etc.) 现在我只需要简单的旧ADO .NET(SqlClient,SqlCommand等)

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? 鉴于我会知道本地机器上当前保存PDF文件的路径,实现此目的的基本语法是什么?

EDIT: Just seen in the MSDN documentation that Image is going to be removed . 编辑:刚刚在MSDN文档中看到Image将被删除 Better use use VARBINARY(MAX). 更好地使用VARBINARY(MAX)。 (Hey I did not know that). (嘿,我不知道那个)。

MY OLD ANSWER: VARBINARY has a limit of 8 KB. 我的老答案: VARBINARY的限制为8 KB。 Can be too small to store PDF's. 可能太小而无法存储PDF文件。 You'd better use the IMAGE datatype. 您最好使用IMAGE数据类型。

Use parameters to insert/select your data via the SqlCommand, and you can use just plain SQL like any ordinary field. 使用参数通过SqlCommand插入/选择数据,您可以像任何普通字段一样使用纯SQL。

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. 编辑2:由于您使用的是C#,因此您应该使用DataReader检索数据。 To create the DataReader use the SequentialAccess flag on the SqlCommand.ExecuteReader , to prevent the data being read into memory too early. 要创建DataReader,请使用SqlCommand.ExecuteReader上的SequentialAccess标志,以防止数据过早地被读入内存。

I really wouldn't use a byte[] parameter to an IDbCommand at all. 我真的不会将一个byte[]参数用于IDbCommand What if your PDF is 100 Mb big? 如果你的PDF是100 Mb大怎么办? 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. 据我所知,在将结果发送到请求的浏览器之前,这将从您的数据库中获取所有100 Mb(在服务器上需要大量内存)。 Now think about getting a number of concurrent requests for different PDFs of roughly this size... per second... That is not very scalable. 现在考虑获得大约这个大小的不同PDF的大量并发请求......每秒...这不是很可扩展。

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. 有关可能的解决方案,请查看“Blob + Stream = BlobStream” ,这是我几年前在荷兰为.NET杂志撰写的一篇文章。 This method uses SQL Server commands to access parts of a BLOB from a Stream derived class. 此方法使用SQL Server命令从Stream派生类访问BLOB的各个部分。

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. 此外,Peter De Jonghe似乎已经在CodeProject上撰写了一篇文章,文章对我的文章进行了一些扩展,将其概括为不仅仅是SQL Server中的image列。 This article is in English. 这篇文章是英文的。

I hope this helps you with your problem. 我希望这可以帮助您解决问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在Excel Server 2008的varbinary(max)列中插入/检索Excel文件? - How do I insert/retrieve Excel files to varbinary(max) column in SQL Server 2008? 如何在 SQL Server 中存储和检索 varbinary(max) 列 - How to Store and Retrieve a varbinary(max) Column in SQL Server 如何将Excel文件插入到具有varbinary类型的列到SQL表中? - How to insert an Excel file to a column with varbinary type into an SQL table? 如何将 Byte[] 转换为数据以在 SQL Server 的 varbinary(max) 列中插入值? - How convert a Byte[] to a data to insert a value in a varbinary(max) column in SQL Server? 检索存储为SQL Server Express数据库中VarBinary的视频文件 - Retrieve video file stored as VarBinary in SQL Server Express database 如何在列中将数据表保存到SQL数据库中,然后在c#中以表格格式检索转换为pdf的数据表 - How to save a datatable to SQL database in a column and later retrieve that convert to pdf in a tabular format in c# 将DataTable存储在SQL的Varbinary列中,然后将Varbinary取回DataTable - Store DataTable in a Varbinary column in SQL and retrieve Varbinary back into DataTable 如何在SQL Server 2008中存储和检索.pdf,.exe文件 - How to store and retrieve .pdf, .exe file in SQL Server 2008 SQL Server查询与varbinary类型列不匹配 - SQL Server query not matching for a varbinary type column 从 SQL 服务器检索保存为 varbinary(max) 的 pdf 附件并使用 C# 保存到本地驱动器的最佳方法 - Best way to retrieve pdf attachments saved as varbinary(max) from SQL Server and save to a local drive using C#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM