读取与存储图片到sqlserver数据的image类型字段
'把照片从数据库中读出
Public Sub get_tmp_pic(rs As ADODB.Recordset, Picture)
Dim img() As Byte
img = rs("图片").GetChunk(rs("图片").ActualSize)
If Dir(App.Path & "/pic.tmp") <> "" Then
Kill (App.Path & "/pic.tmp")
End If
Open App.Path & "/pic.tmp" For Binary As #1
Put 1, 1, img()
Close #1
Picture.Picture = LoadPicture(App.Path & "/pic.tmp")
End Sub
'把图片文件放进数据库中:
Public Sub load_pic(rs As ADODB.Recordset, Form As Object)
Dim bit() As Byte
Dim fn As String
Form.Dialog1.Filter = "BMP文件(*.BMP)|*.BMP|PCX文件(*.PCX)|*.PCX|JPG文件(*.JPG)|*.JPG|"
Form.Dialog1.ShowOpen
If Len(Form.Dialog1.FileName) = 0 Then
Exit Sub
End If
fn = Form.Dialog1.FileName
Open fn For Binary As #1
ReDim bit(LOF(1)) As Byte
Get 1, 1, bit
Close 1
rs("图片").AppendChunk bit
rs.Update
End Sub
'把照片从数据库中读出
Public Sub get_tmp_pic(rs As ADODB.Recordset, Picture)
Dim img() As Byte
img = rs("图片").GetChunk(rs("图片").ActualSize)
If Dir(App.Path & "/pic.tmp") <> "" Then
Kill (App.Path & "/pic.tmp")
End If
Open App.Path & "/pic.tmp" For Binary As #1
Put 1, 1, img()
Close #1
Picture.Picture = LoadPicture(App.Path & "/pic.tmp")
End Sub
'把图片文件放进数据库中:
Public Sub load_pic(rs As ADODB.Recordset, Form As Object)
Dim bit() As Byte
Dim fn As String
Form.Dialog1.Filter = "BMP文件(*.BMP)|*.BMP|PCX文件(*.PCX)|*.PCX|JPG文件(*.JPG)|*.JPG|"
Form.Dialog1.ShowOpen
If Len(Form.Dialog1.FileName) = 0 Then
Exit Sub
End If
fn = Form.Dialog1.FileName
Open fn For Binary As #1
ReDim bit(LOF(1)) As Byte
Get 1, 1, bit
Close 1
rs("图片").AppendChunk bit
rs.Update
End Sub
本文介绍如何使用VBA实现图片在SQL Server数据库中的存储与读取。包括将图片文件存储到数据库Image字段的方法及从数据库读取出图片并显示的具体步骤。
3万+

被折叠的 条评论
为什么被折叠?



