将图像上传到vba excel中的二进制types的图像actvex控制

目前,我在VBA Excel中工作,我试图从SQL Server中导入图像(.png)。 我已经完全把图像插入到二进制types的SQL Server中。 这是我所做的sql查询。

INSERT INTO [DemoDatabase]。[dbo]。[well_image]([well_img])SELECT BulkColumn FROM openrowset(Bulk'C:\ Users \ Pictures \ 003.png',Single_Blob)as img

然后我试图select从SQL Server中获取图像,并显示在图像的ActiveX控件,但它给了我一个错误。

运行时错误,所需的对象。 (错误)

在下面我提供您的概述的代码。

Set rs = New ADODB.Recordset Set stm = New ADODB.Stream Dim sqlquery As String sqlquery = "SELECT [well_img] FROM well_image WHERE [img_id] = 2" rs.Open sqlquery, objConnection, adOpenStatic, adLockOptimistic stm.Type = adTypeBinary stm.Open stm.Write rs("well_img").Value 'write bytes to stream stm.Position = 0 'Sheet1.OLEObjects("well_img").Object.Picture = stm.Read 'load bytes into image control on form Sheet1.well_img.Picture = stm.Read ***here is the problem stm.Close rs.Close objConnection.Close 

当我debugging时,这行是“Sheet1.well_img.Picture = stm.Read”的问题。 我需要你的帮助。 请帮助我,谢谢你的考虑。

我没有真正的解决scheme,但我不相信你可以这样加载图片。 从msdn :

注意在运行时,Picture属性可以设置为任何其他对象的DragIcon,Icon,Image或Picture属性,也可以将其分配给LoadPicture函数返回的graphics。 此例外是ListImages对象的Picture属性,它是一个只读属性。

LoadPicture函数也将文件path作为参数。 本文介绍如何存储和加载Access数据库中的图像 。 不幸的是,这些都不能解决你的问题,但是我希望它能告诉你为什么不能按照你的方式去做。

这么晚才回复很抱歉。 我已经尝试了所有你给出的解决scheme,但仍然无法正常工作。 然后我尝试用另一种方式来做到这一点。 即使这不是回答这个问题,但我只是想分享我已经完成了。


现在我将图像数据types设置为string/ varchar。 这是因为我打算保存图像名称,图像本身保存到我设置的特定文件夹。 这就是我如何做到这一点。

代码


 Dim imgDir as String, imgName as String imgDir = "C:\Users\pc\Pictures\img\" imgName = "test.jpg" 'copy img from source directory to destination directory Dim destinationImg As String destinationImg = "C:\Users\pc\Pictures\img\" & imgName 'FileCopy is a method that copy file to specific folder/directory FileCopy imgDir, destinationImg '----------------- 'insert imgName to database 'set up connection Call serverConn Dim sqlquery As String sqlquery = "INSERT INTO well_img ([img_name]) " & _ "VALUES ('" & imgName & "') " objConnection.Execute sqlquery ' close connection objConnection.Close Set objConnection = Nothing 

这是我显示图像的方式。 我使用图像的ActiveX控件。

 Dim destinationImg As String destinationImg = "C:\Users\pc\Pictures\img\" & imgName Sheet2.dwnload_img.Picture = LoadPicture(destinationImg) 

这一切都来自我。 欢迎任何改进,谢谢大家。 🙂