下面的文件是我已經成功地使用所謂的BlobToFile功能。我還發布了我用來測試它的代碼。圖片被轉儲到一個所謂的臨時文件,但它不是真正的臨時文件,因爲它不在臨時目錄中。您可以手動刪除圖像文件,否則您必須將其寫入臨時文件夾。然後我有一個圖像控件,用於顯示圖片。
Private Sub Command1_Click()
Dim r As DAO.Recordset, sSQL As String, sTempPicture As String
sSQL = "SELECT ID, PictureBlobField FROM MyTable"
Set r = CurrentDb.OpenRecordset(sSQL, dbSeeChanges)
If Not (r.EOF And r.BOF) Then
sTempPicture = "C:\MyTempPicture.jpg"
Call BlobToFile(sTempPicture, r("PictureBlobField"))
If Dir(sTempPicture) <> "" Then
Me.imagecontrol1.Picture = sTempPicture
End If
End If
r.Close
Set r = Nothing
End Sub
'Function: BlobToFile - Extracts the data in a binary field to a disk file.
'Parameter: strFile - Full path and filename of the destination file.
'Parameter: Field - The field containing the blob.
'Return: The length of the data extracted.
Public Function BlobToFile(strFile As String, ByRef Field As Object) As Long
On Error GoTo BlobToFileError
Dim nFileNum As Integer
Dim abytData() As Byte
BlobToFile = 0
nFileNum = FreeFile
Open strFile For Binary Access Write As nFileNum
abytData = Field
Put #nFileNum, , abytData
BlobToFile = LOF(nFileNum)
BlobToFileExit:
If nFileNum > 0 Then Close nFileNum
Exit Function
BlobToFileError:
MsgBox "Error " & Err.Number & ": " & Err.Description, vbCritical, _
"Error writing file in BlobToFile"
BlobToFile = 0
Resume BlobToFileExit
End Function
來源
2011-10-14 21:14:12
HK1
您應該使用'varbinary(max)'作爲'image'已棄用:http://stackoverflow.com/questions/444072/varbinary-vs-image-sql-server-data-type-to-store-binary -data –
這就是訪問給我的。 – johnny
只是爲了闡明,當一個OLE字段綁定到一個窗體中的「綁定OLE控件」,並且一個文件被粘貼到該控件中時,該文件就存儲在一個二進制訪問特定的OLE包裝器中。該控件在顯示文件/圖像時也期望包裝器,但如果該文件是用代碼而不是粘貼保存到表中的,則包裝器不在那裏。在下面的答案中使用未綁定的控件繞過了這個問題,因爲這些控件不需要OLE包裝器。 –