Google Search

Thursday, October 6, 2011

Demonstrate how to upload image into SQL Server by using standard HTML upload methods and then insert each image as a byte array into Sql Server.


Aim:-    Demonstrate  how to upload image into SQL Server by using standard HTML upload methods and then insert each image as a byte array into Sql Server.

Source code:-
                          
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

namespace UploadImages1
{
  public class WebForm1 : System.Web.UI.Page
   {
      protected System.Web.UI.WebControls.TextBox imageDescription;
      protected System.Web.UI.WebControls.Button UploadImage;
      protected System.Web.UI.HtmlControls.HtmlInputFile ImageToUpload;
     
      private void Page_Load(object sender, System.EventArgs e)
      {
      }

            #region Web Form Designer generated code
            override protected void OnInit(EventArgs e)
            {
                  InitializeComponent();
                  base.OnInit(e);
            }
           
            private void InitializeComponent()
            {   
this.UploadImage.Click += new                System.EventHandler(this.UploadImage_Click);
                this.Load += new System.EventHandler(this.Page_Load);

            }
            #endregion

private void UploadImage_Click(object sender,              System.EventArgs e)
            {
            string ContentType = ImageToUpload.PostedFile.ContentType;
            int Length = System.Convert.ToInt32(ImageToUpload.PostedFile.InputStream.Length);
            byte[] Content = new byte[Length];
                       
                  ImageToUpload.PostedFile.InputStream.Read(Content,0,Length);

SqlConnection Connection = new SqlConnection     ("server=localhost;uid=sa;pwd=;database=ImageUpload");
SqlCommand Command = new SqlCommand("INSERT Into Images       (Description, ImageFile, ImageSize, ImageType)  Values (@Description, @ImageFile, @ImageSize, @ImageType)", Connection);

      sqlParameter imageDescriptionParameter = new SqlParameter    ("@Description", SqlDbType.NVarChar);
      imageDescriptionParameter.Value = imageDescription.Text;
      Command.Parameters.Add(imageDescriptionParameter);

                 
      SqlParameter imageFileParameter = new SqlParameter("@ImageFile",  SqlDbType.Image);
          imageFileParameter.Value = Content;
          Command.Parameters.Add(imageFileParameter);

                 
      SqlParameter imageSizeParameter = new SqlParameter("@ImageSize", SqlDbType.Int);
          imageSizeParameter.Value = Length;
          Command.Parameters.Add(imageSizeParameter);

                 
       SqlParameter imageTypeParameter = new SqlParameter("@ImageType", SqlDbType.NVarChar);
            imageTypeParameter.Value = ContentType;
            Command.Parameters.Add(imageTypeParameter);

                 
            Connection.Open();
           
            Command.ExecuteNonQuery();
                 
            Connection.Close();
           
            }
      }
}
view Image program

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

namespace UploadImages1
{
      public class ViewImages : System.Web.UI.Page
      {
                  private void Page_Load(object sender, System.EventArgs e)
            {
            SqlConnection Connection = new SqlConnection      ("server=localhost;uid=sa;database=ImageUpload;pwd=;");
            SqlCommand Command = new SqlCommand("Select ImageID, Description From Images", Connection);
                       
            Connection.Open();
            Response.Write("<H2 align='center'><FONT color='blue'>Image List from SQL Server</FONT></H2><BR><BR>");
            SqlDataReader myDR = Command.ExecuteReader();
            while (myDR.Read())
            {
             Response.Write("<a href='GetImage.aspx?imageid=" + myDR["ImageID"] + "'>" + myDR["Description"] + "</a><br>");
            }
            }

            #region Web Form Designer generated code
            override protected void OnInit(EventArgs e)
            {
                  InitializeComponent();
                  base.OnInit(e);
            }
           
            private void InitializeComponent()
            {   
                  this.Load += new System.EventHandler(this.Page_Load);

            }
            #endregion
      }


Output:-


Result:- The  programme is executed successfully without  any errors.

0 comments:

Post a Comment