Retrieving pictures from SQL Server in asp.net

Source: Internet
Author: User
Reading a picture is much simpler than storing it. Output a picture all we have to do is use the BinaryWrite method of the Response object.
Format the picture at the same time. In this article, we'll discuss how to retrieve a picture from SQL Server. And will learn the following several aspects of knowledge.

• How do I format a picture?
• How to use the BinaryWrite method.
We've stored the data in the person table, so we write code to read the data from the table.
The following code retrieves all the values from the person table.
The code that reads the picture from SQL Server.
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
and see how he works?
The example above is very simple. All we do is execute an SQL statement and then loop through all the records (looping through all the records).
Before displaying the picture, we set the contenttype of the picture, and then we use the BinaryWrite method to output the picture to the browser.
Source:
Retriving.aspx
<%@ Page language= "vb"%>
<%@ Import namespace= "System.Data"%>
<%@ Import namespace= "System.Data.SqlClient"%>
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.