Reading images simply more than storage images. Output a picture of the picture we have to do is the BinaryWrite method using the Response object.
Set the format of the picture at the same time. In this article, we will discuss how to retrieve images from SQL Server. And will learn the following knowledge.
· How to set the picture format?
· How to use a binarywrite method.
We have stored data in the Person table, then we write some code to read data from the table.
The following code retrieves all values from the Person table.
Read the code from the 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) TrymyConnection.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 Success Retrieved!) Catch Sqlexc As SqlexceptionResponse.write (" Read Failed: "& Sqlexc.toTString ()) End Tryend Sub
See how he works?
The above example is simple. What we do is to perform a SQL statement, read all records (looping through all the records).
Before displaying the picture, we set the contentType of the picture first, and then we use the binarywrite method to output the image to the browser.
Source code:
/// Retriving.aspx
<% @ Page language = "vb"%> <% @ Import name "%> <% @ import namespace =" system.data.sqlclient "%>