One of my friends is working on one project which used to store images and some document files. He is using BLOB fields to get this task done, when he was discussing this with me, I suggested him to use FileStream feature instead of the way he is using currently. There are some reasons for that and I am going to explore it in this article.
In SQL Server 2005, we had two option to deal with binary data (image, document etc.).
1.) Store images/documents in OS’ file system and keep pointer (path) of that images/documents into database table
2.) Use BLOB fields to store images/documents directly in database table.
So far we used to take any of the above approaches but both have their own limitations. In first approach, transactional consistency is the issue, when you take backup of database, it won’t get backup of the folders where we are keeping our files (images/documents). In second approach, you can store binary data directly in SQL Server database table so whenever you get database backup, it will backed up by its own but it affects performance while converting that binary data from database table itself, other than that, BLOB field has limitation of 2GB.
To overcome these limitations, Microsoft developer team provided very cool feature, named “Filestream” in SQL Server 2008. With help of “Filestream”, you can store images/documents/videos directly in windows NTFS file system, it has no limitations of 2GB like BLOB and when you take backup of database, your Filestream data will be backed up by its own. Apart from that, you can get advantage of NTFS streaming APIs for efficient and performance driven file operation.
To create Filestream enable column in your table, you have to have a file group in your database which is enable for “FileStream” and you will also need one column in your table which should have varbinary(max) data type so that images/documents/videos could be stored there.
Even before doing any of the above things, you have to enable “Filestream” in your SQL Server which is by default disable.
–if file stream is not already enable in your server
–look at following link to enable it.
EXEC sp_configure filestream_access_level, 2
–why I have used 2? know it from below link.
CREATE DATABASE SQLHubFileStream ONPRIMARY
NAME = SQLHubFileStream_data,
FILENAME = N’D:\TestDB\SQLHubFileStream_data.mdf’
FILEGROUP SQLHubFileStream_FS CONTAINS FILESTREAM
FILENAME = N’D:\TestDB\SQLHubFileStream_FS’
NAME = SQLHubFileStream_LOG,
FILENAME = N’D:\TestDB\SQLHubFileStream_log.ldf’
CREATE TABLE Customers
ID UNIQUEIDENTIFIER ROWGUIDCOL NOT NULL UNIQUE,
CustPhoto VARBINARY(MAX) FILESTREAM
–I have already kept “Ritesh-Teerth.JPG” file in my D drive,
–you have to provide your own path and file name here.
INSERT INTO Customers
(NEWID(),‘Ritesh Shah’,(select * FROM OPENROWSET(BULK ‘D:\Ritesh-Teerth.JPG’, SINGLE_BLOB) AS img))
select * from Customers
Reference: Ritesh Shah
Note: Microsoft Books online is a default reference of all articles but examples and explanations prepared by Ritesh Shah, founder of http://www.SQLHub.com
Ask me any SQL Server related question at my “ASK Profile”
Microsoft SQL Server Blog. Fight the fear of SQL with SQLHub.com. Founder is Ritesh Shah