ASP.NET數據庫圖片存儲到Sql2000中

上篇:圖片存入到Sql Serversql

在不少時候,咱們有這樣的需求:把圖片存入到數據庫當中。在一些應用程序中,咱們可能有一些敏感的資料,因爲存儲在文件系統(file system)中的東西,將很容易被某些用戶盜取,因此這些數據不能存放在文件系統中。數據庫

在這篇文章中,咱們將討論怎樣把圖片存入到Sql2000當中。數組

在這篇文章中咱們能夠學到如下幾個方面的知識:瀏覽器

1. 插入圖片的必要條件ide

2. 使用流對象oop

3. 查找準備上傳的圖片的大小和類型sqlserver

4.怎麼使用InputStream方法?學習

ASP.NET數據庫圖片存儲:插入圖片的必要條件spa

在咱們開始上傳以前,有兩件重要的事咱們須要作:orm

#Form 標記的 enctype 屬性應該設置成 enctype="multipart/form-data"

# 須要一個<input type=file>表單來使用戶選擇他們要上傳的文件,同時咱們須要導入 System.IO名稱空間來處理流對象

把以上三點應用到aspx頁面。同時咱們須要對SqlServer作如下的準備。

# 須要至少含有一個圖片類型的字段的表

# 若是咱們還有另一個變字符類型的字段來存儲圖片類型,那樣會更好一些。

如今,咱們準備了一個Sql表(包含了一個image數據類型的字段),還有<input type=file>標記。固然咱們還得準備Submit按鈕,以便用戶在選擇了圖片之後提交。在這個按鈕的Onclick事件裏,咱們須要讀取選取圖片的內容,而後把它存入到表裏。那咱們先來看看這個Onclick事件。

提交按鈕的Onclick事件的代碼:

  1. Dim intImageSize As Int64  
  2.      Dim strImageType As String  
  3.      Dim ImageStream As Stream 
  4.  
  5.     ’ Gets the Size of the Image  
  6.     intImageSize = PersonImage.PostedFile.ContentLength 
  7.  
  8.     ’ Gets the Image Type  
  9.     strImageType = PersonImage.PostedFile.ContentType 
  10.  
  11.     ’ Reads the Image  
  12.     ImageStream = PersonImage.PostedFile.InputStream 
  13.  
  14.     Dim ImageContent(intImageSize) As Byte  
  15.     Dim intStatus As Integer  
  16.     intStatus = ImageStream.Read(ImageContent, 0, intImageSize) 
  17.  
  18.     ’ Create Instance of Connection and Command Object  
  19.     Dim myConnection As New SqlConnection(ConfigurationSettings.AppSettings("ConnectionString"))  
  20.     Dim myCommand As New SqlCommand("sp_person_isp", myConnection) 
  21.  
  22.     ’ Mark the Command as a SPROC  
  23.     myCommand.CommandType = CommandType.StoredProcedure 
  24.  
  25.     ’ Add Parameters to SPROC  
  26.     Dim prmPersonImage As New SqlParameter("@PersonImage", SqlDbType.Image)  
  27.     prmPersonImage.Value = ImageContent  
  28.     myCommand.Parameters.Add(prmPersonImage) 
  29.  
  30.     Dim prmPersonImageType As New SqlParameter("@PersonImageType", SqlDbType.VarChar, 255)  
  31.     prmPersonImageType.Value = strImageType  
  32.     myCommand.Parameters.Add(prmPersonImageType) 
  33.  
  34.     Try  
  35.         myConnection.Open()  
  36.         myCommand.ExecuteNonQuery()  
  37.         myConnection.Close() 
  38.        Response.Write("New person successfully added!")  
  39.     Catch SQLexc As SqlException  
  40.         Response.Write("Insert Failed. Error Details are: " & SQLexc.ToString())  
  41.     End Try 

這是怎麼實現ASP.NET數據庫圖片存儲的呢?

PersonImage是HTMLInputFile控件的對象。首先須要得到圖片的大小,可使用下面的代碼實現:

intImageSize = PersonImage.PostedFile.ContentLength

而後返回圖片的類型使用ContenType屬性。最後,也是最重要的事就是取得Image Stream,這能夠用如下代碼實現:

ImageStream = PersonImage.PostedFile.InputStream

咱們須要一個字節型數組來存儲image 內容。讀取整個圖片可使用Stream對象的Read方法來實現。Read(in byte[] buffer,int offset,int count)方法有三個參數。他們是:

buffer

字節數組。此方法返回時,該緩衝區包含指定的字符數組,該數組的 offset 和 (offset + count) 之間的值由從當前源中讀取的字節替換。

offset

buffer 中的從零開始的字節偏移量,今後處開始存儲從當前流中讀取的數據。

count

要從當前流中最多讀取的字節數。

這個Read方法用如下代碼實現:
intStatus = ImageStream.Read(ImageContent, 0, intImageSize)
.

如今,咱們已經讀取了整個圖片的內容,下一步,咱們要把這些內容存入到sql 表。咱們將使用存儲過程來完成插入圖片類型和圖片內容到sql 表。若是你瀏覽了上面的代碼,你將會發現咱們使用了sqldbtype.image的數據類型(datatype)。Ok了,完成了這些,咱們也就成功的把圖片存入到SqlServer中了。

下面是咱們編寫的aspx頁面。

ASP.NET數據庫圖片存儲:圖片存入數據庫結論

咱們已經討論瞭如何把圖片存入到Sql Server,那麼咱們如何從SqlServer中讀取圖片呢?

 

下篇:從SqlServer中讀取圖片

 

 

這篇文章是我寫的"如何把圖片存入 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>

相關文章
相關標籤/搜索