先在项目中引用Microsoft.Office.Interop.Access.Dao
代码如下:
Imports Microsoft.Office.Interop.Access.Dao
Public Class Form1
Public Sub Access2010()
Dim DatabasePath As String = "C:\Database1.accdb"
Dim AccessEngine As New DBEngine
Dim db As Database = AccessEngine.OpenDatabase(DatabasePath)
Dim rstable As Recordset = db.OpenRecordset("表1")
'往ID为1的附件列中添加文件:test1.jpg
'读取表的记录
While Not rstable.EOF
'取ID为1的记录
If rstable("ID").Value = 1 Then
'获取附件列的文件集合
Dim rsatts As Recordset2 = rstable("body").Value
'往附件列添加文件
rstable.Edit()
rsatts.AddNew()
rsatts("FileData").LoadFromFile("C:\test1.jpg")
rsatts.Update()
rstable.Update()
End If
rstable.MoveNext()
End While
'将ID为1的附件列中的所有附件保存出来
rstable = db.OpenRecordset("表1")
'读取表的记录
While Not rstable.EOF
'取ID为1的记录
If rstable("ID").Value = 1 Then
'获取附件列的文件集合
Dim rsatts As Recordset2 = rstable("body").Value
While Not rsatts.EOF
'将文件保存出来
rsatts("FileData").SaveToFile("C:\Documents\" & rsatts("FileName").Value)
rsatts.MoveNext()
End While
End If
rstable.MoveNext()
End While
End Sub
End Class