Brian Brian
Brian Brian

Reputation: 163

Insert Image into Database in Visual Studio

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?

Upvotes: 0

Views: 6274

Answers (2)

Dean Kuga
Dean Kuga

Reputation: 12119

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

Upvotes: 2

huMpty duMpty
huMpty duMpty

Reputation: 14460

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

Upvotes: 0

Related Questions