This tutorial will show you how to save image to a SQL database using ASP.NET and C#.

This tutorial needs the sample database provided by MS SQL.

Database:Northwind Table:Categories

Please modify the connectionstring based on the environment of your computer. The sample as below:

Then, you will need to import the System.IO and System.Data.SqlClient namespace.

The System.IO namespace contains the FileInfo and FileStream Classes. They transfer image or file to stream and save to database.

By clicking button of ‘Upload’, we will save the selected image into database.

By clicking button of ‘Show latest Image’, the application will show the latest image from database on the page of Default.aspx

The front end Default.aspx page looks something like this:

The flow for the code behind page is as follows.

Download Source Files