'使用ADODB.Stream来保存/读取图像文件到数据库
'引用Microsoft ActiveX Data Objects 2.5 Library及以上版本
'保存文件到数据库中
Sub SaveFile()
Dim Stm As New ADODB.Stream
Dim Cnn As New ADODB.Connection
Dim rs As New ADODB.Recordset
Dim strCnn As String
strCnn = "Provider=Microsoft.Jet.OLEDB.4.0Persist Security Info=FalseData Source=" &_
App.Path &"\DB1.mdb"
Cnn.Open strCnn
'读取文件到内存(二进制模式)
With Stm
.Type = adTypeBinary
.Open
.LoadFromFile App.Path + "\Image1.bmp"
End With
With rs
.Open "SELECT * FROM TABLE1", Cnn, 1, 3
.AddNew
.Fields("IMAGE") = Stm.Read
.Update
End With
rs.Close
Stm.Close
Set rs = Nothing
Set Cnn = Nothing
Set Stm = Nothing
End Sub
'从数据库中读取图像文件
Sub ReadFile()
Dim Stm As New ADODB.Stream
Dim Cnn As New ADODB.Connection
Dim rs As New ADODB.Recordset
Dim strCnn As String
strCnn = "Provider=Microsoft.Jet.OLEDB.4.0Persist Security Info=FalseData Source=" &_
App.Path &"\DB1.mdb"
Cnn.Open strCnn
rs.Open "SELECT IMAGE FROM TABLE1 WHERE ID = 18", Cnn, adOpenKeyset, adLockReadOnly
'保存到文件
With Stm
.Mode = adModeReadWrite
.Type = adTypeBinary
.Open
.Write rs("IMAGE")
.SaveToFile App.Path + "\Image2.bmp"
End With
'显示图片
Picture1.Picture = LoadPicture(App.Path + "\Image2.bmp")
rs.Close
Stm.Close
Set rs = Nothing
Set Cnn = Nothing
Set Stm = Nothing
End Sub
如果读的时候是按照顺序读的,可以使用超列的表项索引+1得到数据库 对应列表当前选中的记录号,(不按顺序读自己想办法保存记录号 如使用超列表项的值,不保存就祈祷你的数据库中没有重名数据 直接枚举也行 就是效率低 资源占用多), 执行删除 *** 作
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)