仅仅提供思路,下面所有代码都不连贯是缺失的
一,用stream流的方式操作
1,保存数据
Dim stm As New ADODB.Stream ’定义个流 前期绑定ado
'如果后期绑定 CreateObject("ADODB.Stream")
strFileName = Application.GetOpenFilename( _
"图片文件(*.bmp;*.jpg;*.gif),* bmp;*.jpg;*.gif")
If strFileName <> False Then
With stm
.Type = adTypeBinary '二进制
.Open
.LoadFromFile strFileName '读取按照文件方式
End With
'之前sql已经提取出对应文件
mrsADOPic("照片") = stm.Read '写到照片字段里
mrsADOPic.Update '更新
end if
重点就中间stm那段 把文件变成二进制,其实不只图片可以,其他文件也一样可以
2,读取
Private Function StreamToPicture(ByVal id)
Dim strPath$
Dim Conn As ADODB.Connection
Dim Rst As ADODB.Recordset
Dim sql As String
Dim StmPic As New ADODB.Stream '定义图片流
Dim StrPicTemp As String '临时图片
Set Conn = New ADODB.Connection
strPath = ThisWorkbook.Path & "\员工管理.accdb"
Conn.Open "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & strPath
sql = "select * from 员工档案 where 员工编号=" & id
Set Rst = New ADODB.Recordset
StmPic.Type = adTypeBinary '指定流是二进制类型
StmPic.Open '将数据获取到Stream对象中
Rst.Open sql, Conn, 1, 3
Debug.Print Rst.RecordCount
'读取显示数据库中的图像
StrPicTemp = ThisWorkbook.Path & "\" & "temp.bmp"
If Rst.RecordCount = 1 And IsNull(Rst.Fields("照片").Value) &