views:

385

answers:

4

Hey,

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. 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.

+2  A: 

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

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:

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. Between 256K and 1M, the read:write ratio and rate of object overwrite or replacement are important factors.

Mitch Wheat
What about double backups etc?
Robert Koritnik
+1 for the reference to the MS research paper. I remember reading that several years ago.
RichardOD
@Robert Koritnik: what about them? you still have to keep them in sync.
Mitch Wheat
Would the downvoter please leave a comment. Thanks.
Mitch Wheat
@Mitch: If you store files on a filesystem you have to create a separate backup from your DB backup. Syncing both may be a nightmare. There are also other (security) issues with storing files directly on a filesystem.
Robert Koritnik
@Mitch: Nowadays there are new techniques and solutions to these problems. Your data here is a bit outdated, but still valuable though.
Robert Koritnik
@Robert Koritnik: erm, Robert that's what I'm saying in my answer. You seeem to have it the wrong way round!
Mitch Wheat
@Robert Koritnik: I'm aware of FILESTREAM in SQL 2008, Poster is basically saying what if I don't have SQL 2008.
Mitch Wheat
+1  A: 

Don't load DBMS by binary data! Let SQL Server do its specific work. It is better when files stored in file system and even on other computer.

gt_rocker
please see http://research.microsoft.com/apps/pubs/default.aspx?id=64525
Mitch Wheat
A: 

My preferred solution is always to save them to a directory on your file system and save the url / location in the database. 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.

Stuart
It also has the drawback, that your backups of the DB and filesystem have to be in sync.
Mitch Wheat
@Mitch - agreed - but a minor point imo
Stuart
@Stuart: depending on the app. This may be imperative obstacle for certain applications.
Robert Koritnik
@Robert - ok, agreed, as always the case it's whatever is the best solution for each scenario.
Stuart
+4  A: 

Saving files on filesystem but inside a 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

With SQL Server 2008 Microsoft introduced a new data storage mechanism called FILESTREAM that stores data in folders as regular files. 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. 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
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. That's the only reason this answer is related to version 2008, because this really is the optimal solution for their problem.

Robert Koritnik
+1. This is what I recommend also.
RichardOD
Yes, providing you can use SQL Server 2008.
RichardOD
@RichardOD: I asked him about version 2008 and he said he may use it. So I assume this IS the best solution.
Robert Koritnik
The poster has siad he has SQL Server 2005
Mitch Wheat
As well as admitting he can use version 2008. Check his comments under his own question.
Robert Koritnik