处理.NET IO流管理(在SQL Server中编码和存储)
我想知道以下信息:
1)我需要,对于我的应用程序,处理IO流。特别是,我需要获取一个文件并将其分成几个二进制片段以在网络上发送。我可以使用 .NET API 执行此操作吗?
2)我还需要将这些片段保存在某个地方。我对将片段保存在数据库(SQL Server)中非常感兴趣。 SQL Server 是否提供特殊的表字段类型来保存二进制数据???
谢谢
I would like to know the following please:
1) I need, for an application of mine, to handle IO streams. In particular, I need to take a file and split it into several binary fragments to send on the network. Can I do this with .NET API?
2) I also need to save this fragments somewhere. I am really interested in saving fragments in a database (SQL Server). Does SQL Server provide special table field types for saving binary data???
Thank you
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是的。
是的。
Yes.
Yes.
对于 SQL Server(版本 2005 或更高版本)中的二进制数据,请使用
VARBINARY
数据类型 - 请参阅 有关二进制和 varbinary 的 MSDN 文档 了解详细信息。使用
VARBINARY(MAX)
数据类型,您可以在数据库表的列中存储最多 2 GB 的二进制数据。正如微软研究院这篇非常好的论文,名为To Blob or Not To Blob 还表明,您不应该在 SQL Server 中过度存储二进制数据。
他们经过大量性能测试和分析后得出的结论是:
如果您的图片或文档大小通常低于 256K,则将它们存储在数据库 VARBINARY 列中效率更高
如果图片或文档大小通常超过 1 MB,将它们存储在文件系统中效率更高(并且使用 SQL Server 2008 的 FILESTREAM 属性,它们仍然处于事务控制之下并且是数据库的一部分)
如果您决定将图片放入 SQL Server 表中,我强烈建议使用单独的表来存储这些图片 - 不要将员工照片存储在员工表中- 将它们放在单独的表中。这样,假设您并不总是需要选择员工照片作为查询的一部分,那么 Employee 表就可以保持精简、简洁且非常高效。
对于文件组,请查看文件和文件组体系结构了解简介。基本上,您可以从一开始就为大型数据结构创建具有单独文件组的数据库,或者稍后添加其他文件组。我们称之为“LARGE_DATA”。
现在,每当您要创建一个需要存储 VARCHAR(MAX) 或 VARBINARY(MAX) 列的新表时,您都可以为大数据指定此文件组:
查看有关文件组的 MSDN 介绍,并使用它!
For binary data in SQL Server (in version 2005 or newer), use the
VARBINARY
datatype - see the MSDN docs on binary and varbinary for details.With the
VARBINARY(MAX)
datatype, you can potentially store up to 2 GB of binary data in a column in your database table.As this really good paper by Microsoft Research called To Blob or Not To Blob also shows, you shouldn't overdo your binary storage in SQL Server.
Their conclusion after a large number of performance tests and analysis is this:
if your pictures or document are typically below 256K in size, storing them in a database VARBINARY column is more efficient
if your pictures or document are typically over 1 MB in size, storing them in the filesystem is more efficient (and with SQL Server 2008's FILESTREAM attribute, they're still under transactional control and part of the database)
in between those two, it's a bit of a toss-up depending on your use
If you decide to put your pictures into a SQL Server table, I would strongly recommend using a separate table for storing those pictures - do not store the employee foto in the employee table - keep them in a separate table. That way, the Employee table can stay lean and mean and very efficient, assuming you don't always need to select the employee foto, too, as part of your queries.
For filegroups, check out Files and Filegroup Architecture for an intro. Basically, you would either create your database with a separate filegroup for large data structures right from the beginning, or add an additional filegroup later. Let's call it "LARGE_DATA".
Now, whenever you have a new table to create which needs to store VARCHAR(MAX) or VARBINARY(MAX) columns, you can specify this file group for the large data:
Check out the MSDN intro on filegroups, and play around with it!