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:
所以,我们有这个:
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:
现在我们有了这个:
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:
因此,上面的内容如下所示:
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图片控件,而是较新的图像控件。这是功能区中的图像控件:
更多回答
我是一名优秀的程序员,十分优秀!