Search Suggest

SQL Server, how to save images and attachments in the database. Is better to store images into the database or into the filesystem?

Hi Guys,

This is the first post of the year and i want to do my very best wishes for a great 2022! I hope it's a year of joy, peace and happiness for everyone!

Well, this year we will start with a question! 

Where is it better to save images and attachments? ...within the database or on the file system?

yes but, some might ask. 

How can I save images and attachments in the database? ..easy.

Enjoy the read mates!


Read and save images and attachments into the database

Suppose we have a table that contains data relating to our products.
One possibility is to store the image of our product in a column.

In our example the table will have this structure:


CREATE TABLE Products (
Id int identity(1,1) PRIMARY KEY NOT NULL,
Code Varchar(20),
ImageProduct VarBinary (MAX)
)

Our image will be stored into the ImageProduct field of type VarBinary.

Obviously in that same field we can store everything we want, a binary file, an application, an attachment and so on ..

The first things to remember is to enable the OLE Automation Procedures using the following command:


Use master
Go
EXEC sp_configure 'show advanced options', 1;
GO
RECONFIGURE;
GO
EXEC sp_configure 'Ole Automation Procedures', 1;
GO
RECONFIGURE;
GO
ALTER SERVER ROLE [bulkadmin] ADD MEMBER sa
GO


Now, if everything is alright, you should be able to execute an Openrowset command.

You should put a valid file name instead of the file indicated by me!


SELECT * FROM Openrowset( Bulk 'c:\scambio\pistone-cagiva-alettaelefant-125cc-2-tempi.jpg', Single_Blob) as img

This is the result. Data are inside the BulkColumn VarBinary field.

Select * from Openrowset bulk

And now the final step.

we insert the data in our table:


INSERT INTO Products (code,ImageProduct)
SELECT
'0001',BulkColumn
FROM
Openrowset( Bulk 'c:\scambio\pistone-cagiva-alettaelefant-125cc-2-tempi.jpg', Single_Blob) as img

 

Wasn't it easy?

We have just read a file (an image) from the filesystem and stored inside the database.
 
Now we need to read our file from the database, so we could directly view it (via a program) or save it back to the file system.

To read the file from our table it is sufficient to extract the data


SELECT ImageProduct FROM Products

Saving our file (or image) to a file system is just a bit more complicated because we have to use OLE Automation.

This technology provides some methods to read and save files (open, write, savetofile)

After reading the data and storing it in the variable @ImageProduct we open an ADODB stream.
The two highlighted lines write to the stream and save it to file

DECLARE @ImageProduct VarBinary(MAX);
DECLARE @OleObj INT;

SELECT @ImageProduct = ImageProduct FROM Products

EXEC sp_OACreate 'ADODB.Stream' ,@OleObj OUTPUT;
EXEC sp_OASetProperty @OleObj ,'Type',1;
EXEC sp_OAMethod @OleObj,'Open';
EXEC sp_OAMethod @OleObj,'Write', NULL, @ImageProduct;
EXEC sp_OAMethod @OleObj,'SaveToFile', NULL, 'c:\scambio\newfile.jpg', 2;

EXEC sp_OAMethod @OleObj,'Close';
EXEC sp_OADestroy @OleObj;


 
 
Here it is! now you can read and write binary data such as images or other into the database!


Filesystem Vs. Database, small considerations..

So, where is better store images, attachments or other binary files? 

There are of course pros and cons.

If you store files on a database you won't have to worry about path anymore, so if you move the database your files will follow it. 

While you do one backup of your data you will also automatically have a copy of your files and this is also very nice.

The database however will growth quicky!

On the other side the file system was born to manage files, so if the files to be managed are very large, perhaps it is more practical to use the file system.

In any case after this post, if you want to store them in your database ... you know how!

 

That's all for this first post of the year!
Keep following me here on the blog and on linkedin.













Help me to share knowledge on my blog  

Next post:

Post a Comment