如何使用Microsoft Access从Sql Server显示图像? [英] How do I display an image from Sql Server with Microsoft Access?
问题描述
我将Access 2007数据库升级为SQL Server 2008 R2。图像在SQL Server中作为图像类型。 Access具有指向包含图像的表的链接。当我尝试从Access中显示时,它不会这样做。它仍然具有OLE对象包装器。
I upsized an Access 2007 database to SQL Server 2008 R2. The images are in SQL Server as image type. Access has link to the table containing the image. When I try to display from within Access, it won't do it. It still has the OLE Object wrapper.
如何获取该图像并将其显示在Access中的表单上?目前我没有选择删除图像,将它们放在目录中并指向它们(我知道的最好的方法,但不是一个选项)。我需要直接从SQL Server读取image / blob文件并将其显示在Access表单上。
How can I get that image and display it on my forms in Access? I do not have the option, at the moment, to remove the images, put them in a directory and point to them (the best way I know but not an option). I need to read the image / blob file directly from SQL Server and display it on an Access form.
感谢您的任何想法。
我看到了这一点,但没有帮助:
I saw this but it did not help:
http://access.bukrek.net/documentation 看起来像文件夹方法中的文件
http://access.bukrek.net/documentation looks like the file in folder method
推荐答案
下面是我成功使用的一个名为BlobToFile的函数。我还发布了用于测试它的代码。图片被转储到所谓的临时文件,但它不是真正的临时文件,因为它不在临时目录中。您可以手动删除图像文件,否则您必须将其写入临时文件夹。然后我有一个图像控件,我在那里显示图片。
Below is a function I have successfully used called BlobToFile. And I also posted the code that I use to test it. The picture gets dumped to a so-called temp file but its not truly temp because it isn't in the temp directory. You can manually delete the image file or else you'll have to write it to your temp folder instead. Then I have an image control where I display the picture.
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
这篇关于如何使用Microsoft Access从Sql Server显示图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!