[轉載]在ASP.Net中從sqlserver檢索(retrieve)圖片

來源:互聯網
上載者:User
介紹:
這篇文章是我寫的"如何把圖片存入sqlServer中"的後續。我建議你在讀這篇文章之前先看看那篇。
和儲存圖片相比,讀取圖片就要簡單多了。輸出一副圖片我們要做的就是使用Response對象的BinaryWrite方法。
同時設定圖片的格式。在這篇文章中,我們將討論如何從SqlServer中檢索圖片。
並將學習以下幾個方面的知識.
·如何設定圖片的格式?
·如何使用BinaryWrite方法。

我們已經在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

看看他是怎麼工作的?
上面的例子很簡單。我們所作的就是執行一個sql語句,再迴圈讀取所有的記錄(looping through all the records).
在顯示圖片之前,我們先設定了圖片的contentType,然後我們使用BinaryWrite方法把圖片輸出到瀏覽器。

原始碼:
/// retriving.aspx

<%@ Page Language="vb" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<HTML>
  <HEAD>
    <title>Retrieving Image from the Sql Server</title>
        <script runat=server>
                        Public Sub Page_Load(sender As Object, e As EventArgs)
                                    ' Create Instance of Connection and Command Object
                                    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    

    </script>
  </HEAD>
  <body style="font: 10pt verdana">
  </body>
</HTML>

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.