asp.net| Insert | data | Database writing this article is because today I saw csdn on the first page of a significant error, called "Retrieving (retrieve) a picture from SQL Server in ASP.net". The mistake is not to say it is because the method can actually read out the image from the database and display in the browser, the fault is because the intent of the code can not be fully implemented, the author also seems to have a smattering of HTTP protocol and the process of processing HTTP data.
1. How to make Mistakes
The following are the methods mentioned in this article:
Public Sub Page_Load (sender as Object, E as EventArgs)
Dim MyConnection as New SqlConnection (ConfigurationSettings.AppSettings ("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 a picture of the Personimage field stored in all records in the person table to the browser at once, and print out "person info" under the output picture if the output succeeds successfully retrieved! " Information. In fact, however, the above code only correctly prints the picture in the first record. For browsers, an HTTP request acquires a file (HTML or picture), so the output of the above code will be used as a file (type based on Response.ContentType = Mydatareader.item ("Personimagetype ) is processed by the browser. If the corresponding type of HTTP is a picture such as Image/jpeg, the browser uses the corresponding image parsing feature to parse the picture file. As a result, the above code can only be displayed as a picture of the first record personimage field. The subsequent record output of the picture data will become the redundant data of the first picture (this point is universal, but not absolute, depending on the format of the picture), thereby following the "person info successfully retrieved! "The information is naturally not shown, because the information is already encoded in the image file.
2, the right way
A, the image into the database, the following is a picture into the database code snippets: (Complete demo program See appendix I)
This code can be placed in the Page_Load event, the data picture to note that two points are:
First, set the correct contenttype (Content-type in HTTP), the picture Content-type format is generally image/*, such as JPEG for image/jpeg,bmp image/bmp and so on.
Second, only output a picture binary flow, ASP.net Page_Load event before the page output is triggered, so the image output can be carried out in this event, direct Operation Reponse Object, to avoid the output of the picture-independent information (additional second picture or text). The binary stream output of the picture is used in time to end the HTTP response using the Response.End () method to avoid the extra information in the page being exported by the ASP.net engine to the client.
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.