The correct method for inserting images into the database and reading them using asp.net

Source: Internet
Author: User

This article is written because I saw an article on the CSDN homepage named "retrieve images retrieved from sqlserver in Asp. Net" that has a major mistake. The error is not mentioned because the method can indeed read the image from the database and display it in the browser. It is said that the error is because the intent of the Code cannot be fully implemented, the author also seems to understand the http protocol and the process in which the browser processes http data.
1. How to make an error
The methods mentioned in this article are as follows:
Public Sub Page_Load (sender As Object, e As EventArgs)
Dim myConnection As New SqlConnection (ConfigurationSettings. etettings ("ConnectionString "))
Dim myCommand As New SqlCommand ("Select * from Person", myConnection)
Try
MyConnection. Open ()
Dim myDataReader as SqlDataReader
MyDataReader = myCommand. ExecuteReader (CommandBehavior. CloseConnection)
Do While (myDataReader. Read ())
Response. ContentType = myDataReader. Item ("PersonImageType ")
Response. BinaryWrite (myDataReader. Item ("PersonImage "))
Loop
MyConnection. Close ()
Response. Write ("Person info successfully retrieved! ")
Catch SQLexc As SqlException
Response. Write ("Read Failed:" & SQLexc. ToString ())
End Try
End Sub
Obviously, programmers want to output the images stored in the PersonImage field of all records in the Person table to the browser at one time, in addition, if the output is successful, the "Person info successfully retrieved!" is printed under the output image!" Information. However, in fact, the above Code only outputs the image in the first record correctly. For the browser, an http request is used to obtain an object (html or image), so the output of the above Code will be used as a file (type based on Response. contentType = myDataReader. items ("PersonImageType") are processed by the browser. If the http type is image/jpeg, the browser uses the corresponding image parsing function to parse the image file. Therefore, the display result of the above Code can only be the image of the first record PersonImage field. The output image data recorded later will become the redundant data of the first image (This point is universal, but not absolute, depending on the image format ), the following "Person info successfully retrieved!" Because the information is already encoded in the image file.

Related Article

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.