C# Store and Retrieve Images in SQL Database with Hashing

This simple Windows Forms application in C# tutorial will show you how to store & retrieve images from SQL Database using Binary hashing instead of image linking with source code & demo. I had this topic on my list long ago so today I decided to publish it. There are many scenarios in which we need to save images in MS SQL Database with Hash Code. It is easy for professionals but new programmers find it hard to achieve first time. I myself encountered this situation in one of my semester projects.

Save and Retrieve Images via Hashing

First of all I want to describe hashing in my own words

Hashing of image is a useful way to reduce a huge amount
of data to a number that can be used to identify that image.

So as you can guess we will convert the specified image into a number & then store that number in our Database. Many new programmers do a silly mistake here. They save the image path in database. The mistake is if that image is removed from that particular folder or even if its name is changed then you can not retrieve that image. But with Hashing we can store our image completely there in Database because it will be treated as other data types.

Table in SQL Database

So first of I will create a table in SQL database. (if you already have any table where you will save that image omit this step).

C# Store and Retrieve Images in SQL Database with Hashing
C# Store and Retrieve Images in SQL Database with Hashing

So I created a database named ‘MyTests’ in which I add a table ‘UserInfo’ which contain ‘UserPicture’ column. This column will store our images. Did you notice the Datatype of this column? . It is in varbinary(MAX) . It is because hashing will convert the image in a very big Binary number.

Windows Form Application

Now I have to design my windows form to our need.

C# Store and Retrieve Images in SQL Database with Hashing
C# Store and Retrieve Images in SQL Database with Hashing

Here User will input detail & select image which will be loaded in Picture Box & then will click on Add user to database.

Source Code

Saving Image in SQL Database

Retrieving Image from SQL Database


In first part we first created object of Memory Stream class which will help in converting image in Raw format & then storing it in a byte Array. After it we run the SQL Command to insert it in database.

In second part we just converted the binary number back into the array & eventually forming image back. For the complete working code please download the application from below.


Here is the video output of this application. Click on the below image to play video.


Download complete application with code here. Remember to just change the value of ‘MyDatabasePath’ with your own database.

Size: 54 KB
Visual Studio 2010 Files

2 thoughts on “C# Store and Retrieve Images in SQL Database with Hashing”

  1. awesome. This is best. Thanks . I have been looking for this from so much time. It works perfectly specially the working application that you have attached here is best. Again thnaks.

Comments are closed.