SQL Server 2017 Hosting – Store And Retrieve Using FILESTREAM From SQL Database

Today I will explain regarding storing and retrieving images, videos and other document files from SQL server using the FILESTREAM feature. If you are new to FILESTREAM then read my previous article FILESTREAM Feature In SQL Server. We are taking one example with WPF application and SQL server 2008 for understanding.

First we are creating one database with FILESTREAM. Enable option then create one table with FILESTREAM column. Script for creating table is given below.

We are creating one WPF window application with image control, buttons and text box as shown in below image.

In this application you have to browse image file using browse button and then give some name to this image in text box and click on “Save photo To Database” button this will insert new records in database. After inserting record you have to click on “Load Photo from Database” button. This button will take last inserted image data from database and display in below image control. Below I have given XAML design code for creating above UI in WPF application.



For browsing image you have write the below code on button click event.

Now for storing an image file in database, first we are creating table with file stream enable column. Script for creating Table is given below.

Now for storing the image we have to use below insert query.


In above query you have to pass local image path. If your SQL server has installed on different PC and your image is on local path then you have to give network path in above insert query.

For inserting a record in database we have write below code on “Save Photo To Database” button click event.


After successfully inserting an image in database, we have to retrieve image data from the database and display it in image control in WPF application.

Whatever data you have retrieved from the database it in byte array format so first you have to convert this byte array data to memory stream and then you have to pass this memory stream to the image control shown in below.


As you seen in above code first we are fetching record from database using select query and then we are assigning database content to byte array and then we are creating memory stream from this byte array.

Then create BitmapImage from the memory stream and give this BitmapImage to image control.

In the above example we have shown the storing and retrieving image from the database but If you have any video file or any other document file then inserting of video or PFD file are same as image file, so you have to give path of that file. But when you want to retrieve any file from database then you have to create file from byte array using below code.

  1. public void ByteArrayToFile(string fileName, byte[] byteArray)
  2. {
  3.      try
  4.      {
  5.                 using (var fs = new FileStream(fileName, FileMode.Create, FileAccess.Write))
  6.                 {
  7.                     fs.Write(byteArray, 0, byteArray.Length);
  8.                 }
  9.      }
  10.      catch (Exception)
  11.      {
  12.      }
  13. }

Using this above function you can create file from your byte array. You can use the above function  for creating a PDF file, video file, or any document files.

In the above function you have to provide path where you have to save your file.