0

I have a asp.net site I'm creating in Visual Studio and am stumped as to how I should store images in my database.

I have done some research and am under the impression that storing images in directories and then storing their references in the database is superior to using BLOB's. However, I can't find any good tutorials on how to do this via Visual Studio, and am completely new to creating directories/referencing them.

At some point I want to allow users to add images tied to specific database entries, and will also have many images/banners all around my site, so I'm pretty sure BLOB isn't preferable. Are there any best practices for creating and referencing directories, and how should I go about doing so?

huMpty duMpty
  • 14,346
  • 14
  • 60
  • 99
Brian Brian
  • 163
  • 1
  • 3
  • 17
  • You don't specify which database you're using; if it's SQL Server 2008 or later, `FILESTREAM` is a better choice than either byte columns or "manual" file system management. It uses the file system, but it's handled automatically by the database engine. – Aaronaught Sep 05 '13 at 18:39

2 Answers2

2

In .NET every file has FullName property which is basically a full path to the file in the file system.

This is a string of maximum 260 characters so all you need to do is store this full path in nvarchar(260) column and when ever you need to access the image just query the DB in your code to get this full path and use it to display or manipulate (File.Copy, File.Delete, File.Move) the file.

If you intend to programmatically create folders to store images just use the Directory.CreateDirectory Method or to check if directory exists use Directory.Exists Method.

EDIT: As Jeff Cuscutis pointed out the absoulte path is usually not the best choice so the solution is to use a UNC path instead (\\ComputerName\ShareName\DirectoryName\FileName.extension) where ComputerName and ShareName could be variables stored in settings or elsewhere...

Dean Kuga
  • 11,878
  • 8
  • 54
  • 108
  • Sorry @DeanK, I read the question too quickly and thought OP wanted images **in** the database. I've deleted my answer, and posted the content here as comments. – criticalfix Sep 05 '13 at 17:40
  • This [link from the "related" sidebar to the question](http://stackoverflow.com/questions/12055132/save-image-in-database-visual-studios?rq=1) shows how to pull an image out of a file into a byte array, and wire it to a parameter that you can use to insert the image into a field in a database table. The database column type should be VARBINARY(MAX). – criticalfix Sep 05 '13 at 17:41
  • I agree with the commenters who say you should keep the images on the file system, and only store the file paths (or urls) in the database. Databases are good at connecting many small pieces of information, and they do it very quickly. File systems, e.g. folders and directories on disk, are good at storing large objects like image files, and they can return these image files to you very quickly. Use databases for the things databases are good at, and use file systems for the things file systems are good at. – criticalfix Sep 05 '13 at 17:42
  • The FullName property may not be the best as it makes the application non-portable, requiring the same folder structure on all machines. I prefer relative paths or having an images folder setting with the path and just filenames for the images in the database. YMMV. – Jeff Cuscutis Sep 05 '13 at 18:30
0

Basically there are two options. You can save the path to the file or you can directly save the file.

Not quite sure which database you are using, but this is a sql-server example

How to store image in SQL Server database tables column

Also there is a good exmple about How to Store or Save images in SQL Server

Community
  • 1
  • 1
huMpty duMpty
  • 14,346
  • 14
  • 60
  • 99