Upload Image in SQLServer using C#

Posted by in C# category on for Beginner level | Points: 250 | Views : 10617 red flag

Lets look into how to upload Image in SQLServer database using asp.net

Upload Image in SQLServer using C#

Here we are going to see how to save image in SQLServer database using asp.net. It’s very common task to save image or other file in database in daily project development routine. So being  asp.net developer it’s very necessary to know “How to save image in sqlServer database.

Let’s create step to step to solve the problem.

1)     Create table in SQLServer

It will be our first step. We will create a simple table to save data in SQLServer.

CREATE TABLE [dbo].[imageSave1]([id] [int] IDENTITY(1,1) NOT NULL,[FileContent] [image] NULL,[ImageFileName] [varchar](50) NULL)

This script will create table in sql server database. There are three fields in this table.

First fieldà It’s primary key of table

Second fieldàIt will contain actual image and it’s data type is Image.

Third fieldà It’s nothing by filename of inserted file.

This is only table setup , needed to perform image insert operation.

2)     Create aspx page to desing UI

In this step we will configure aspx page by drag and drop one file uploader control  in our aspx page. In below I have given aspx page content.

Page Language="C#" AutoEventWireup="true" CodeFile="InDatabase.aspx.cs" Inherits="_Default"
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <form id="form1" runat="server">
    <asp:FileUpload ID="FileUpload1" runat="server" />
    <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />

The output view is here.

This design is pretty simple. Only I have kept one file uploader control and one button within body of HTML html page. And this button is for trigger upload code. When user will click button it will fire code to save image in database.

3)     Write C# code to save data in database

This is the last step of Implementation. Here we will write code to save Image in database.  At first we will convert Image to byte array then as normal data we will pass this byte array through query to save it In database. In below I have given C# code.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Diagnostics;
public partial class _Default : System.Web.UI.Page
    protected void Page_Load(object sender, EventArgs e)
    protected void Button1_Click(object sender, EventArgs e)
        int length = FileUpload1.PostedFile.ContentLength;
        String Filename = FileUpload1.PostedFile.FileName;
        byte[] imgbyte = new byte[length];
        HttpPostedFile img = FileUpload1.PostedFile;img.InputStream.Read(imgbyte, 0,length);
        SqlConnection con = new SqlConnection();
        con.ConnectionString = "Data Source=SERVERNAME;Initial Catalog=test;Integrated Security=True";
        SqlCommand cmd = new SqlCommand();
        cmd.CommandText = "insert into imageSave1(FileContent,ImageFileName)values('" + imgbyte + "','"+ Filename + "')";
        cmd.Connection = con;

The code is pretty simple. Within button click event at first we are taking file from uploader control, and then we are converting it as byte array. At last the ADO.NET code is taking responsibility to save data in database.

Page copy protected against web site content infringement by Copyscape

About the Author

Full Name: Sourav Kayal
Member Level: Silver
Member Status: Member,MVP
Member Since: 6/20/2013 2:09:01 AM
Country: India
Read my blog here http://ctrlcvprogrammer.blogspot.in/
I am .NET developer working for HelixDNA Technologies,Bangalore in healthcare domain. Like to learn new technology and programming language. Currently working in ASP.NET ,C# and other microsoft technologies.

Login to vote for this post.

Comments or Responses

Login to post response

Comment using Facebook(Author doesn't get notification)