gpt4 book ai didi

Cannot display OLEDB photos from SQL Server in MSAccess form or report(无法在MSAccess窗体或报表中显示来自SQL Server的OLEDB照片)

转载 作者:bug小助手 更新时间:2023-10-24 19:51:44 25 4
gpt4 key购买 nike



I have begun to transfer a small database from MSAccess to SQL Server. The database contains photographs and that is the cause of my problem.
Working in MSAccess, I attached, via ODBC, to the SQL Server database.
I opened dbo.person and dragged and dropped some .jpg photographs from an external directory into the photo field in the table dbo.Person. The photo field is of type varbinary(max).
I have proven that the photographs are in fact stored in the photo field by double clicking the photo field. This displays each photograph on my screen.
My problem:-
I do not know how to display the photos, which are in OLEDB format, in an MSAccess form or report.
I would greatly appreciate any assistance, thank you,
mccer

我已经开始将一个小型数据库从MSAccess传输到SQL Server。数据库里有照片,这就是我的问题所在。在MSAccess中工作时,我通过ODBC连接到了SQL Server数据库。我打开dbo.Person并将一些.jpg照片从外部目录拖放到表dbo.Person中的照片字段中。照片字段的类型为VARBINARY(Max)。通过双击照片字段,我已经证明照片实际上存储在照片字段中。这会在我的屏幕上显示每一张照片。我的问题:-我不知道如何在MSAccess窗体或报表中显示OLEDB格式的照片。如果您能帮忙,我将不胜感激,谢谢您,麦克斯尔


I tried tube and searching Microsoft site.

我试了试管和搜索微软网站。


更多回答

I have never had good experience using OLEObject field and controls for dynamic display of images in Access. Never tried using SQLServer field. Easier to leave images external and store file path in text field, then use Image control for dynamic display by setting ControlSource to field.

我从来没有在Access中使用OLEObject字段和控件动态显示图像的良好体验。从未尝试使用SQLServer字段。更容易将图像留在外部,并将文件路径存储在文本字段中,然后通过将ControlSource设置为字段来使用图像控件进行动态显示。

what june 7th said. I haven't done anything with pictures in a while so i could be wrong, but it seems oleObject is the problem. Switch the photos to the attachment type (very annoying) and they will display in an image control. see my answer here for more details: stackoverflow.com/questions/69474390/…

6月7日说的话。我已经有一段时间没有对图片做任何事情了,所以我可能是错的,但似乎OleObject才是问题所在。将照片切换到附件类型(非常烦人),它们将显示在图像控件中。有关更多详细信息,请参阅此处的答案:Stackoverflow.com/Questions/69474390/…

优秀答案推荐

I'm somewhat surprised that drag + drop into a form with an oleDB control works with SQL Server.

我有点惊讶的是,拖放到带有olDB控件的表单中可以与SQL Server一起使用。


However, I would VERY but VERY strong suggest you don't use oleDB embedding. The reason for this is many, but one big reason is that other software, even web based or say other desktop programs will fair much better if you simply save the file as raw byte data into that varbinary(max) column in SQL Server.

但是,我强烈建议您不要使用oleDB嵌入。这样做的原因有很多,但一个很大的原因是,如果您只需将文件作为原始字节数据保存到SQL Server中的varinary(Max)列,其他软件,甚至是基于Web的或其他桌面程序将会更好。


This also tends to use the least amount of space. Image compression technology is REALLY amazing, often compressing an image by 10 or more times.

这也倾向于使用最少的空间。图像压缩技术真的很神奇,通常会将图像压缩10倍或更多。


The issue with oleDB embedding is that the bmp un-compressed preview image is often LARGER than the whole high-resolution picture you saved in the database!

OlDB嵌入的问题是,BMP未压缩的预览图像通常比您在数据库中保存的整个高分辨率图片都大!


So, this means we need several moving parts for this to work.

因此,这意味着我们需要几个运动部件才能使其工作。


First, we need to pop a file browse dialog for the user to select the picture file (sorry, no drag + drop can be used).

首先,我们需要弹出一个文件浏览对话框供用户选择图片文件(对不起,不能使用拖放)。


Next, we need some code to read the file from disk as raw bytes, and shove this result into the varbinary(max) column.

接下来,我们需要一些代码来从磁盘读取文件作为原始字节,并将此结果放入varbinary(max)列。


Last, but not least, you need "one extra" line of code in a form to display the picture from the database.

最后但并非最不重要的一点是,您需要在表单中使用“额外的”代码行来显示数据库中的图片。


This also means we will use the new picture control. This new control (introduced into Access 2010) is rather nice since it can render most picture formats, and that includes rendering data from SQL Server varbinary(max) columns.

这也意味着我们将使用新的图片控件。这个新的控件(引入到Access 2010中)相当不错,因为它可以呈现大多数图片格式,其中包括从SQL Server varinary(Max)列呈现数据。


So, assuming the form is based on a linked table to SQL Server, and one column is varbinary(max).

因此,假设该表单基于一个指向SQL Server的链接表,并且其中一列是varinary(Max)。


So, we will have to pop the file dialog to browse to a picture.

因此,我们将不得不弹出文件对话框来浏览图片。


So, let's drop in a text box (called txtFile). And beside that a button to browse to a file.

因此,让我们放入一个文本框(名为txtFile)。旁边还有一个浏览文件的按钮。


So, we have this:

所以,我们有这个:


enter image description here


And the code behind for the select file button is this:

SELECT FILE按钮后面的代码如下:


  Private Sub cmdFile_Click()

Dim f As FileDialog
Set f = Application.FileDialog(msoFileDialogFilePicker)

f.Show

If f.SelectedItems.Count > 0 Then
Me.txtFile = f.SelectedItems(1)
End If

End Sub

And now we have this:

现在我们有了这个:


enter image description here


Now, beside the above, I have a button to save the picture selected.

现在,在上面的旁边,我有一个按钮来保存所选的图片。


In "production" code no doubt these 2 steps would be combined into a simple and single file selection, save and display.

在“生产”代码中,这两个步骤无疑会组合成一个简单的单一文件选择、保存和显示。


So, beside above, after we select the file, we have a save to database button.

因此,在上面,在我们选择文件后,我们有一个保存到数据库按钮。


That code is this:

代码是这样的:


  Private Sub cmdSaveToDB_Click()

Dim MyImageB() As Byte

MyImageB = GetFileBytes(Me.txtFile)

Me!MyImage = MyImageB
Me.Dirty = False

Me.Image1.PictureData = Me.Recordset!MyImage

End Sub

And one more routine:

还有一个例行公事:


  Public Function GetFileBytes(ByVal path As String) As Byte()

Dim lngFileNum As Long
Dim bytRtnVal() As Byte
lngFileNum = FreeFile

If LenB(Dir(path)) Then ''// Does file exist?
Open path For Binary Access Read As lngFileNum
ReDim bytRtnVal(LOF(lngFileNum) - 1&) As Byte
Get lngFileNum, , bytRtnVal
Close lngFileNum
Else
Err.Raise 53
End If

GetFileBytes = bytRtnVal

Erase bytRtnVal

End Function

So, all of the above routines can be used for the desired result.

因此,上面的所有例程都可以用于预期的结果。


So, I broken this out to multiple steps, and as noted, in practice, the user would browse to a picture, select it, and then code would save to database and display it, and do so in one operation.

因此,我将其分解为多个步骤,正如前面提到的,在实践中,用户将浏览到一张图片,选择它,然后代码将保存到数据库并显示它,并且只需一次操作即可完成此操作。


So, above looks like this:

因此,上面的内容如下所示:


enter image description here


Note that if the form has/allows record navigation, then you need to add to the forms on-current event since the new image control does not automatic display the image for you, even if you bind the new image control to the column behind (hence, don't bind the image control - leave it un-bound and use code to load/fill the picture).

请注意,如果表单具有/允许记录导航,则需要添加到Forms on-Current事件,因为新的图像控件不会自动为您显示图像,即使您将新的图像控件绑定到后面的列(因此,不要绑定图像控件-保留其未绑定,并使用代码加载/填充图片)。


So, the one line of code, either at form load time (for forms that load to one record), or using form on-current event, you need this one line of code for the picture to display automatic:

因此,这一行代码,无论是在表单加载时(对于加载到一条记录的表单),还是使用表单on-current事件,您都需要这一行代码来自动显示图片:


     Me.Image1.PictureData = Me.Recordset!MyImage

As noted, we are NOT using the older oleDB picture control in the form, but using the newer image control. That is this image control from the ribbon:

如前所述,我们在表单中使用的不是较旧的oleDB图片控件,而是较新的图像控件。这是功能区中的图像控件:


enter image description here


更多回答

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com