這篇文章是我寫的"如何把圖片存入sqlServer中"的后續。我建議你在讀這篇文章之前先看看那篇。
和存儲圖片相比,讀取圖片就要簡單多了。輸出一副圖片我們要做的就是使用Response對象的BinaryWrite方法,同時設置圖片的格式。在這篇文章中,我們將討論如何從SqlServer中檢索圖片。并將學習以下幾個方面的知識:
。保绾卧O置圖片的格式?
。玻绾问褂肂inaryWrite方法。
我們已經在Person表中存儲了數據,那么我們就寫些代碼來從表中讀取數據。下面的代碼檢索了所有的值從Person表中。
從sqlserver中讀取圖片的代碼:
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
文章來源于領測軟件測試網 http://www.kjueaiud.com/