0% found this document useful (0 votes)
10 views4 pages

Upload Images to SQL Server Tutorial

The document describes a C# program that demonstrates how to upload an image file to SQL Server by inserting it as a byte array. The program includes code to: 1) Upload an image file from an HTML form and get the file contents as a byte array 2) Insert the image byte array, description, size and type into a SQL Server database table using ADO.NET 3) Retrieve and display the image descriptions from the database table
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views4 pages

Upload Images to SQL Server Tutorial

The document describes a C# program that demonstrates how to upload an image file to SQL Server by inserting it as a byte array. The program includes code to: 1) Upload an image file from an HTML form and get the file contents as a byte array 2) Insert the image byte array, description, size and type into a SQL Server database table using ADO.NET 3) Retrieve and display the image descriptions from the database table
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd

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 [Link];
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];

namespace UploadImages1
{
public class WebForm1 : [Link]
{
protected [Link] imageDescription;
protected [Link] UploadImage;
protected [Link] ImageToUpload;

private void Page_Load(object sender, [Link] e)


{
}

#region Web Form Designer generated code


override protected void OnInit(EventArgs e)
{
InitializeComponent();
[Link](e);
}

private void InitializeComponent()


{
[Link] += new
[Link](this.UploadImage_Click);
[Link] += new [Link](this.Page_Load);

}
#endregion

private void UploadImage_Click(object sender,


[Link] e)
{
string ContentType = [Link];
int Length =
[Link].ToInt32([Link]);
byte[] Content = new byte[Length];
[Link](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", [Link]);
[Link] = [Link];
[Link](imageDescriptionParameter);

SqlParameter imageFileParameter = new SqlParameter("@ImageFile",


[Link]);
[Link] = Content;
[Link](imageFileParameter);

SqlParameter imageSizeParameter = new SqlParameter("@ImageSize",


[Link]);
[Link] = Length;
[Link](imageSizeParameter);

SqlParameter imageTypeParameter = new SqlParameter("@ImageType",


[Link]);
[Link] = ContentType;
[Link](imageTypeParameter);

[Link]();

[Link]();

[Link]();

}
}
}
view Image program

using System;
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];

namespace UploadImages1
{
public class ViewImages : [Link]
{
private void Page_Load(object sender, [Link]
e)
{
SqlConnection Connection = new SqlConnection
("server=localhost;uid=sa;database=ImageUpload;pwd=;");
SqlCommand Command = new SqlCommand("Select ImageID,
Description From Images", Connection);

[Link]();
[Link]("<H2 align='center'><FONT color='blue'>Image
List from SQL Server</FONT></H2><BR><BR>");
SqlDataReader myDR = [Link]();
while ([Link]())
{
[Link]("<a href='[Link]?imageid=" +
myDR["ImageID"] + "'>" + myDR["Description"] + "</a><br>");
}
}

#region Web Form Designer generated code


override protected void OnInit(EventArgs e)
{
InitializeComponent();
[Link](e);
}

private void InitializeComponent()


{
[Link] += new [Link](this.Page_Load);

}
#endregion
}
Output:-

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

You might also like