VBA EXCEL 实现图片保存到ACCESS数据库(OLE对象)

在网上看了很多教程,VBA怎么把图片通过ACCESS数据库的OLE对象传到数据库。结果全是其他语言写的。我今天用VBA 实现一下以下功能:代码前需引用  Microsoft Activex Data Objects x.x 库

1,把图片保存到ACCESS(通过OLE对象)

Sub 保存图片到ACCESS数据库()
'头像那列数据库类型为OLE对象
Dim con As New ADODB.Connection '
Dim command As New ADODB.command

With con
.Provider = "microsoft.ace.oledb.12.0"
.ConnectionString = "d:\cs.accdb"
.Open
End With
Dim b() As Byte
Open "d:\b.jpg" For Binary As #1
 x = LOF(1)
 ReDim i(1 To x)
Get #1, , b
Close #1
command.ActiveConnection = con
command.CommandType = adCmdText
command.CommandText = "insert into 表1(头像) values(@头像)"
command.Parameters.Append command.CreateParameter("@头像", adBinary, , x, b)
command.Execute
con.Close
Set con = Nothing
End Sub
 

2,把ACCESS数据库中的图片保存到本地硬盘。

Sub 保存图片到本地()

Dim con As New ADODB.Connection
Dim rs As New ADODB.Recordset
Dim b() As Byte
With con
.Provider = "microsoft.ace.oledb.12.0"
.ConnectionString = "d:\cs.accdb"
.Open
End With
Set rs = con.Execute("select * from 表1 where id=7")
b = rs.Fields("头像")
Open "d:\头像.jpg" For Binary As #1
Put #1, , b
Close #1
con.Close
Set con = Nothing
End Sub

已标记关键词 清除标记
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页