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 using using using using using using using using using using
System; System.Collections; System.ComponentModel; System.Data; System.Drawing; System.Web; System.Web.SessionState; System.Web.UI; System.Web.UI.WebControls; System.Web.UI.HtmlControls; 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 using using using using using using using using using using
System; System.Collections; System.ComponentModel; System.Data; System.Drawing; System.Web; System.Web.SessionState; System.Web.UI; System.Web.UI.WebControls; System.Web.UI.HtmlControls; 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("
Image List from SQL Server
"); SqlDataReader myDR = Command.ExecuteReader(); while (myDR.Read()) { Response.Write("
" + myDR["Description"] + ""); } } #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.