ASP.NET 2.0 - 如何把上傳的檔案儲存到資料庫欄位 自章立民CnBlogs)

來源:互聯網
上載者:User
 

許多朋友詢問如何將使用者在網頁上所選取的圖片寫入資料庫欄位,我們撰寫了一個網頁範例來示範如何實作。以下是這一個網頁範例的操作流程:

 

1.           請表 1 所示,按下「先於資料庫中建立所需的資料表」按鈕,此舉會在「北風貿易」資料庫中建立一個名稱為「練習作者」的資料表,以便用來儲存包括照片在內的資料記錄。

 


圖表 1

 

2.           請表 2 所示,按下「瀏覽」按鈕來選取照片。

 


圖表 2

 

3.           請表 3 所示,於「選擇檔案」對話方塊中選取所需的照片並按下「開啟」按鈕。

 


圖表 3

 

4.           請表 4 所示,按下「上傳並寫入資料庫」按鈕。

 


圖表 4

 

5.           反覆上述步驟的操作,直到您已新增完畢所需的各筆資料記錄之後,請表 5 所示,按一下超級連結「檢視資料表的照片資料」以便檢視我們所所上傳並寫入至資料庫欄位的資料。

 


圖表 5

 

瞭解了網頁範例的操作方式之後,我們要來檢視其開發技巧。我想,本範例最關鍵的技巧,就是如何將包括照片在內的資料記錄新增至資料表,此項作業的程式碼撰寫於「上傳並寫入資料庫」按鈕的 Click 事件處理常式中,茲列示如下:

 

Protected Sub btnUpload_Click(ByVal sender As Object, _
 ByVal e As System.EventArgs) Handles btnUpload.Click
 ' 利用 SqlConnectionStringBuilder 對象來構建連接字串。
 Dim connectStringBuilder As New SqlConnectionStringBuilder()
 connectStringBuilder.DataSource = "(local)\SQLExpress"
 connectStringBuilder.InitialCatalog = "北風貿易"
 connectStringBuilder.IntegratedSecurity = True

 Try
      ' 建立串連。
      Using con As New SqlConnection(connectStringBuilder.ConnectionString)

        Dim data() As Byte = _
          My.Computer.FileSystem.ReadAllBytes(Me.File1.Value.Trim())
/*
Dim fileLen As Integer

        ' 取得上傳之照片檔的長度。

        fileLen = FileUpload1.PostedFile.ContentLength

        ' 建立一個位元組數組來持有檔案的內容。
        Dim Input(fileLen) As Byte
        Input = FileUpload1.FileBytes
*/

        Dim updateCMD As SqlCommand = _
        New SqlCommand( _
        "INSERT INTO 練習作者 (作者姓名, 作者玉照) VALUES (@AuthorName, @Photo)", _
        con)

        Dim AuthorNameParameter As SqlParameter = _
          New SqlParameter("@AuthorName", SqlDbType.NVarChar, 10)
        If Me.txtAuthorName.Text.Trim().Equals("") Then
            AuthorNameParameter.Value = "章立民研究室"
        Else
            AuthorNameParameter.Value = Me.txtAuthorName.Text.Trim()
        End If
        updateCMD.Parameters.Add(AuthorNameParameter)

        Dim PhotoParameter As SqlParameter = _
          New SqlParameter("@Photo", SqlDbType.Image)

        PhotoParameter.Value = data
        updateCMD.Parameters.Add(PhotoParameter)
        con.Open()
        updateCMD.ExecuteNonQuery()
        con.Close()

        Me.lblMessage.Text = "成功新增資料記錄"
      End Using
 Catch ex As Exception
      Me.lblMessage.Text = "發生錯誤" & vbCrLf & ex.ToString
      Throw New Exception(ex.Message)
 End Try
End Sub

 

參考資料:
Visual Basic 2005 檔案 IO 與資料存取秘訣 - CH7:探討大型物件(LOB)
的存取秘訣
Visual C# 2005 檔案 IO 與資料存取秘訣 - CH7:探討大型物件(LOB) 的存取秘訣

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.