views:

68

answers:

3

Hi,

in my new c#.net 3.5 ASP Website, user have the possibility to upload a picture.

What solution is the best for saving this picture? Should I save it on my server in a folder and save the path in the database, or should I save the picture in the database?

A: 

Should I save it on my server in a folder and save the path in the database

You could do something like that or...

you can take advantage of the FILESTREAM feature in SQL Server 2008.

Files will be stored on disk but transparently manipulated as though they were directly in a database field. Lessens load on the database and constraints the database explosive growth, especially if you take advantage of NTFS streaming for accessing those files.

An Introduction to SQL Server FileStream

Developer Art
A: 

It is not recommended to store large files/pictures in the DB, but you can. For that use a column of type varbinary.

As of SQL Server 2008, you can also save BLOBs=files on the disk but still have SQL server refer to them with the help of the filestream storage attribute. You can check basics of the performance differences here.

BrokenGlass
I'd favour `image` over `varbinary` unless I knew the images where all going to be very small. `varbinary` is restricted in terms of maximum size.
Jon Hanna
I don't think that's true Jon - image fields are deprecated and completely replaced by varbinary, also see this thread: http://stackoverflow.com/questions/47203/sql-server-nvarcharmax-or-ntext-image-or-varbinary
BrokenGlass
Ah yes. We're both right, image for 2005 for the reasons I said, varbinary(MAX) for 2008 for the reasons you said. Unless the querant is actually using Postgres of Oracle or something, in which case we're both wrong :)
Jon Hanna
+1  A: 

Pro files:

  1. There is a simple call to make to serve a file to the response stream. Just set the ContentType appropriately, call that, and you're done.
  2. Its more performant (though this is often overstated, good use of blobs or SQLServer filestreams tends to be pretty performant, but still lose compared to file operations).

Pro DB:

  1. Consistency is easier, you can't have the situation where the file structure and database mismatch.
  2. File-based solutions are tricky if you move to a web-farm solution with more than one webserver (above a certain amount of success you will have to do this).
  3. Migration of data due to modifications is generally easier. You're going to have to move the DB data somewhere anyway, so once you've solved that you've solved it for images too.
  4. Manipulation code is easier to write.
  5. Reporting on the whole collection of images is easier.
Jon Hanna