Asp. Net image access in SqlServer

Source: Internet
Author: User
Use asp.net to upload and store images to SqlServer, and then read and display images from SqlServer.
1. Upload and store the data to sqlserver
Database Structure
Create table test
{
Id identity (1, 1 ),
FImage image
}
Related stored procedures
Create proc UpdateImage
(
@ UpdateImage Image
)
As
Insert Into test (FImage) values (@ UpdateImage)
GO

Add the following to the upphoto. aspx file:
<Input id = "UpPhoto" name = "UpPhoto" runat = "server" type = "file">
<Asp: Button id = "btnAdd" name = "btnAdd" runat = "server" Text = "Upload"> </asp: Button>

Then, add the btnadd button to the post code file upphoto. aspx. cs and click the event processing code:
Private void btnAdd_Click (object sender, System. EventArgs e)
{
// Obtain the image and convert the image to byte []
HttpPostedFile upPhoto = UpPhoto. PostedFile;
Int upPhotoLength = upPhoto. ContentLength;
Byte [] PhotoArray = new Byte [upPhotoLength];
Stream PhotoStream = upPhoto. InputStream;
PhotoStream. Read (PhotoArray, 0, upPhotoLength );

// Connect to the database
SqlConnection conn = new SqlConnection ();
Conn. ConnectionString = "Data Source = localhost; Database = test; User Id = sa; Pwd = sa ";

SqlCommand cmd = new SqlCommand ("UpdateImage", conn );
Cmd. CommandType = CommandType. StoredProcedure;

Cmd. Parameters. Add ("@ UpdateImage", SqlDbType. Image );
Cmd. Parameters ["@ UpdateImage"]. Value = PhotoArray;

// If you do not want to use the stored procedure to add an image, replace the above four statements with the following code:
// String strSql = "Insert into test (FImage) values (@ FImage )";
// SqlCommand cmd = new SqlCommand (strSql, conn );
// Cmd. Parameters. Add ("@ FImage", SqlDbType. Image );
// Cmd. Parameters ["@ FImage"]. Value = PhotoArray;

Conn. open ();
Cmd. ExecuteNonQuery ();
Conn. Close ();
}

2. Read and display data from sqlserver
Add the following code to the image display area:
<Asp: image id = "imgPhoto" runat = "server" ImageUrl = "ShowPhoto. aspx"> </asp: image>

Showphoto. aspx subject code:
Private void Page_Load (object sender, System. EventArgs e)
{
If (! Page. IsPostBack)
{
SqlConnection conn = new SqlConnection ()
Conn. ConnectionString = "Data Source = localhost; Database = test; User Id = sa; Pwd = sa ";

String strSql = "select * from test where id = 2"; // assume that the image with id 2 is obtained.
SqlCommand cmd = new SqlCommand ()
Reader. Read ();
Response. ContentType = "application/octet-stream ";
Response. BinaryWrite (Byte []) reader ["FImage"]);
Response. End ();
Reader. Close ();
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.