I am programming a website in asp.net Visual Web Developer which I am going to have a lot of product pictures to display on the webpage. Should I store all my images in SQL Server and pull each picture from there or should I store all of the images in a "Picture" folder created inside of my website root folder? Is there a big difference? The Images would be linked to other tables in the database by using the Order_Number this is not a problem.
-
1Outside, and you might want to include a direct link to the created/stored files on top of that. You won't be caught with the database engine reading binary when it should be the web server doing that. – Jaaz Cole Jul 15 '14 at 16:12
-
...or even on a CDN service. Either way, this is very much an opinion based question... – Rowland Shaw Jul 15 '14 at 16:12
-
http://stackoverflow.com/questions/2850337/storing-images-in-sql-server-using-c-sharp?answertab=votes#tab-top – Habib Jul 15 '14 at 16:12
-
Also, http://stackoverflow.com/q/335342/50447 – Rowland Shaw Jul 15 '14 at 16:13
-
So I would just need to make sure that the folder containing all of my images gets uploaded to the web host along with the rest of the website files when that time comes? – user3841709 Jul 15 '14 at 16:22
-
Also, I have other data along with each picture that I will be pulling in dynamically from the database. If the pictures aren't stored in the database as well, does that mean that I will have to develop a new page for each product or is there a way to link the images stored outside of the database to the data in the database? – user3841709 Jul 15 '14 at 16:25
-
Nvm, after reading the link it seems like I should be fine storing the path of the image in the database which I will be able to connect to the rest of my data. Thanks guys – user3841709 Jul 15 '14 at 16:32
-
Yes! C: drive is good. I also sometimes use the D: drive with good results. Or you could use a thumb drive, which might be mounted as E: or some other letter. – Jul 15 '14 at 18:03
-
@torazaburo The choice of which drive letter to use is completely dependent on how the server was setup. And no, storing images on a thumb drive for purposes of serving them over the web is a terrible, horrible idea. – mason Jul 15 '14 at 18:08
-
@mason Oops, forgot to include a smiley in my comment. :-) – Jul 17 '14 at 02:59
2 Answers
Store them on the hard drive, this will allow IIS to cache them and serve them much more efficiently. If you make it so that requesting an image requires invoking a controller IIS cannot cache the image as a static file.

- 1,389
- 2
- 16
- 29
Too long for a comment.
Images in the database -- I know too many people that regret that decision. Just don't do it except perhaps in light duty usage.
Don't store the path of the image in the database. If you ever have to split images into multiple locations you will have a big mess. Ideally you store a unique (string) identifier hash. Then you computer via a shared function to correct location to pull this from based on the hashed name.
For version 1.0 you could just dump everything into a single directory (so your hash to directory function is very simple). Ideally you want the generated name to be "randomly distributed", i.e., as likely to be zq% as an%. You also ideally want it to be short. Unique is a requirement. For example, you could use an identity field - guaranteed unique but not randomly distributed. If you have large numbers of images, you will want to store these in multiple directory -- so you don't essentially lock up your machine if you ever look at this directory with windows explorer.
A good practice is to combine methods. e.g. Make a hashing function that yield 4 characters (perhaps by keeping only 4 characters of output from TSQL HASHBYTES or CHECKSUM (hashing the identity value) and making the short hash the directory name. Now use the identity value as the filename and you have a simple and scaleable design since you can tweak the algorithm down the road if needed.

- 8,831
- 3
- 19
- 41
-
Ok, this information sounds great... it's just a little over my head. I am going to look into it and do a little research on storing unique identifier hash, but real quick, does this essentially work the same as if I were storing the exact path in the database it is just encrypted, in a way? And would that hash be used as the query string or is that bad? – user3841709 Jul 15 '14 at 18:24
-
It is not encrypted, it is randomized. By not storing the full you are prepared for the future. Take your time to understand it. If you don't have large number of image, just use the identity as the filename and put everything in a single directory. You can then convert to this design without major headache. – Gary Walker Jul 15 '14 at 18:29