Retrieved from SQLServer in ASP.NET
Source: www.aspalliance.com
Introduction: This article is follow-up of "how I write" to store pictures into SQLServer ". I suggest you look at that article before reading this article. 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 format of the picture? • 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.
Sqlserver image read from the code. 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 mydataareader as sqldataareader mydatareader = mycommand.executeRead (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 Sqlexception Response.write ("Read Failed:" & Sqlexc.toTString ()) End Sub
See how he works? The above example is simple. What we do is to execute a SQL statement, read all records (Looping Through the records). Before displaying the image, 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 "%>