简体   繁体   English

使用.Net和SQL Server 2005保存/加载图片的最佳方法?

[英]Best way to save/load pictures with .Net & SQL Server 2005?

What is the typical way to handle product pictures in a web-page selling products? 在销售产品的网页中处理产品图片的典型方法是什么? Say I had a database with books or computer components etc, all of which have their own sample pictures for example... Should I save them into the DB as binary data, or somehow handle them in the codebehind, saving them into a directory with the appropriate link to the picture file being saved into the product table in the DB? 假设我有一个包含书籍或计算机组件等的数据库,所有这些都有自己的示例图片...我应该将它们作为二进制数据保存到数据库中,还是以某种方式在代码隐藏中处理它们,将它们保存到一个目录中保存到数据库产品表中的图片文件的相应链接?

I'm trying to make a sales company example, and while I can come up with ways to do this, I wonder what is the typical norm used in the programming business? 我正在尝试以销售公司为例,虽然我能想出办法,但我想知道编程业务中使用的典型规范是什么?

Update: Storing on another system or alternate DB's is not an option, while the information on possible alternatives for future reference is appreciated. 更新:存储在另一个系统或备用数据库上不是一种选择,而有关未来参考的可能替代方案的信息将受到赞赏。 All I have available to are Visual Studio 2008 and SQL Server 2005 / 2008, on one computer, with one ASP.Net project. 我只能在一台计算机上使用一个ASP.Net项目获得Visual Studio 2008和SQL Server 2005/2008。 So the files would have to be saved into a directory within the web-page project, or into the one DB being used by it. 因此,必须将文件保存到Web页面项目中的目录中,或者保存到其使用的一个DB中。

Saving files on filesystem but inside a DB 将文件保存在文件系统上但在DB中
Saving files into DB makes it a single backup point for all your application data and makes a smaller security surface for your app because you don't need a write permission folder for storing files. 将文件保存到数据库使其成为所有应用程序数据的单个备份点,并为您的应用程序创建较小的安全性表面,因为您不需要用于存储文件的写入权限文件夹。

SQL Server 2008 FILESTREAM storage to the rescue SQL Server 2008 FILESTREAM存储到救援

With SQL Server 2008 Microsoft introduced a new data storage mechanism called FILESTREAM that stores data in folders as regular files. 使用SQL Server 2008,Microsoft引入了一种名为FILESTREAM的新数据存储机制,该机制将文件夹中的数据存储为常规文件。 But you can access them (and store their metadata) via tables. 但您可以通过表格访问它们(并存储其元数据)。 You can store files as varbinary(max) and they get stored on the filesystem as configured. 您可以将文件存储为varbinary(max),并将它们存储在配置的文件系统中。 These files will also get picked up by DB Backup procedure. 这些文件也将被数据库备份程序选中。 This way you get the best of both worlds in a single solution. 通过这种方式,您可以在单一解决方案中获得两全其美

Read about it here . 在这里阅读它。

Post-festum note 后festum笔记
I have to point out that authors of the question did mention they may use SQL Server 2008 as well when there are any benefits. 我必须指出,问题的作者确实提到过,当有任何好处时,他们也可以使用SQL Server 2008。 That's the only reason this answer is related to version 2008, because this really is the optimal solution for their problem. 这是这个答案与2008版本相关的唯一原因,因为这确实是他们问题的最佳解决方案。

Please see: Storing a file in a database as opposed to the file system? 请参阅: 将文件存储在数据库中而不是文件系统中?

I have working C# code for SQL Server 2005 if you decide to store in the Database here: Save and Restore Files/Images to SQL Server Database 如果你决定在这里存储数据库,我有SQL Server 2005的C#代码: 保存和恢复文件/图像到SQL Server数据库

This MS Research paper by the late Jim Gray: To BLOB or Not To BLOB: Large Object Storage in a Database or a Filesystem has the following advice: 这篇由已故Jim Gray 撰写的 MS Research论文: 对BLOB或不对BLOB:数据库或文件系统中的大对象存储有以下建议:

As expected from the common wisdom, objects smaller than 256K are best stored in a database while objects larger than 1M are best stored in the filesystem. 正如通常所预期的那样,小于256K的对象最好存储在数据库中,而大于1M的对象最好存储在文件系统中。 Between 256K and 1M, the read:write ratio and rate of object overwrite or replacement are important factors. 在256K和1M之间,读取:写入比率和对象覆盖或替换率是重要因素。

Don't load DBMS by binary data! 不要通过二进制数据加载DBMS! Let SQL Server do its specific work. 让SQL Server完成其特定的工作。 It is better when files stored in file system and even on other computer. 当文件存储在文件系统甚至其他计算机上时,它会更好。

My preferred solution is always to save them to a directory on your file system and save the url / location in the database. 我首选的解决方案是始终将它们保存到文件系统上的目录中,并将URL /位置保存在数据库中。 I find this the easiest to maintain and it's easy to change databases, datastorage without affecting the files. 我发现这是最容易维护的,并且很容易更改数据库,数据存储而不会影响文件。

This method, is not dependent on the database. 此方法不依赖于数据库。

I had a situation recently where a legacy system stored images and files as binary, which caused time and effort retrieving these to upgrade database software. 我最近有一种情况,遗留系统将图像和文件存储为二进制文件,这导致时间和精力检索这些文件以升级数据库软件。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM