Save Image in SQL table in form of image data type


Here is a simple way to store image in sql table as Image data type (for better security). I found the way how we can store our images in the form of bytes in sql table and again retrieve it in the image form.
This is the simple method to store image in sql table:
·         Here is table structure:
CREATE TABLE [dbo].[Photograph](
     [Photograph_Id] [int] IDENTITY(1,1) NOT NULL,
     [Emp_Id] [int] NULL,
     [Photograph] [image] NULL,
     [ImageType] [varchar](100) NULL,
     [ImageSize] [numeric](18, 0) NULL,
)
·         Here is the method to store image in byte form
private void SaveEmployeePhotograph(int empID, FileUpload fuEmployeePhotograph)
    {
        try
        {
            Byte[] imgByte = null;
            string strContentType=string.Empty;
            float flImageSize = 0;
            if (fuEmployeePhotograph.HasFile && fuEmployeePhotograph.PostedFile != null)
            {
                HttpPostedFile photoFile = fuEmployeePhotograph.PostedFile;
                imgByte = new Byte[photoFile.ContentLength];
                photoFile.InputStream.Read(imgByte, 0, photoFile.ContentLength);

                strContentType = photoFile.ContentType;
                flImageSize = photoFile.ContentLength;

                SqlParameter[] pArray = new SqlParameter[4];

                pArray[0] = new SqlParameter("@Emp_Id", SqlDbType.Int);
                pArray[0].Value = empID;
                pArray[1] = new SqlParameter("@Photograph", SqlDbType.Image);
                pArray[1].Value = imgByte;
                pArray[2] = new SqlParameter("@ImageType", SqlDbType.VarChar, 100);
                pArray[2].Value = strContentType;
                pArray[3] = new SqlParameter("@ImageSize", SqlDbType.Float);
                pArray[3].Value = flImageSize;
                objDAL.ExecuteNonQuery("usp_AddPhotograph", pArray);
            }
        }
    }

·         Here is how can we bind the image from table in image format as below (getImage.aspx):
protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.QueryString["empID"] != null)
        {
            SqlParameter[] pArray = new SqlParameter[1];
            pArray[0] = new SqlParameter("@Emp_Id", SqlDbType.Int);
            pArray[0].Value = Convert.ToInt32(Request.QueryString["emp_ID"]);
            DataTable dt = objDAL.GetDataTable("usp_GetPhotograph", pArray);
            if (dt != null)
            {
                Byte[] bytes = (Byte[])dt.Rows[0]["Photograph"];
                HttpContext.Current.Response.Buffer = true;
                HttpContext.Current.Response.Charset = "";
                HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.NoCache);
                HttpContext.Current.Response.AddHeader("content-disposition", "attachment;filename=" + dt.Rows[0]["Photograph_ID"].ToString());
                HttpContext.Current.Response.BinaryWrite(bytes);
                HttpContext.Current.Response.Flush();
                HttpContext.Current.Response.End();
            }
        }
    }

Here we can replace empID=1 to your requirements
<img id="imgPhotograph" src="getImage.aspx?empID=1" width="20" height="16"  alt="click to open" />


I hope it will help you out.

Cheers

Comments