2012-01-25 192 views
0

您好我正在創建一個使用VB.net的Windows應用程序,我想知道如何創建一個下載按鈕,以便當用戶點擊它時,它能夠從數據庫中的附件列下載特定的附件。從MS Access 2007 DB(VB.net)下載文件

我已經試圖谷歌我怎麼可以這樣做,但不幸的是,我無法找到答案。我很感激,如果有經驗的人可以指導我編寫這個函數的過程。

回答

0

使用DAO(添加Microsoft.Office.Interop.Access.Dao.DLL的引用)從Ms-Access數據庫讀取附件字段。

示例代碼:

Imports Microsoft.Office.Interop.Access 
Public Class Form1 
    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click 
     Dim FileName = "c:\WindowsApplication1\Database1.accdb" 

     'Directory location where attachment files will be stored. 
     Dim Path = "c:\WindowsApplication1\" 

     Dim engine As New Dao.DBEngine 
     Dim database As Dao.Database = engine.OpenDatabase(FileName) 

     Dim rs As Dao.Recordset = database.OpenRecordset("select MyAttachmentField from TableName") 
     While Not rs.EOF 
      Dim rs1 As Dao.Recordset2 = rs.Fields("MyAttachmentField").Value 
      While Not rs1.EOF 
       Dim fName As String = Path & rs1("FileName").Value 
       Dim fld As Dao.Field2 = rs1("FileData") 
       'Delete a file if same named file exists 
       System.IO.File.Delete(fName) 
       fld.SaveToFile(fName) 
       rs1.MoveNext() 
      End While 
     rs.MoveNext() 
     End While 
    End Sub 
End Class 
+0

嗨,我是相當新的這個,所以我不太明白這一點。你能提供一些簡單的代碼嗎? –

+0

@ Sheryl-AnnLee - 附件是否以OLE數據類型或文本存儲到數據庫中?如果它是文本,那麼它有一個文件路徑。哪個提供者 - 使用OleDb或ODBC? – adatapost

+0

我使用OleDb,它是一個附件字段,我不知道它是否是OLE數據類型。該字段下有三件事,文件數據文件類型和文件名。 –