SQL - How to store images in a database

In this tutorial, we discuss how to store images in a database.

The video tutorial is linked below -



There are two ways in which images can be stored in a database.

The first is to store the image as  a varbinary in a table. The second method is to store the image location on the file system and then use the front end application to fetch the image from that location.

Though the second method is the preferred method as it does not take up large database space as is true with the first method as the image size can be huge. In turn, storing large amounts of images can take up lots of database space and degrade its performance and response time.

To store the image as a varbinary, the below steps can be performed.

1) Create a column of datatype - varbinary to store the image.

Here we create a table Images and the column Img to store the image -

Create table dbo.Images

(id int null,

Img varbinary(max) null);

2) Use the Openrowset function to access the image from the file system and then insert it in the varbinary column.

Insert into dbo.Images values

(1, (Select * from Openrowset (BULK 'C:\SQL\Images\SQL.png' , Single_Blob) as T))

The detailed explanations of the above statement can be found in the video tutorial linked above.


Post a Comment

Previous Post Next Post

Contact Form