存:
Public Sub save_picture()
Dim cnn As New ADODB.Connection, rst As New ADODB.Recordset
Dim bit() As Byte
dim varPath as string '图片的路径
cnn.open "连接数据库的字符串"
sSql = "SELECT * FROM 表 WHERE 关键字='" & 关键值 & "'"
'选出要增加或修改图片记录的记录
rst.Open sSql, cnn, adOpenKeyset, adLockOptimistic
If Not (rst.EOF And rst.BOF) Then
If VarPath = "" Then
' 然后将字节数组的内容写入数据库即可
rst.Fields("图片") = ""
rst.UPDATE
Else
Open VarPath For Binary As #1
ReDim bit(LOF(1)) As Byte
Get 1, 1, bit
Close 1
' 然后将字节数组的内容写入数据库即可
rst.Fields("图片").AppendChunk bit
rst.UPDATE
End If
End If
end sub
取:
Public Sub show_picture()
Dim REC As Recordset
Dim sSql As String
Dim I As Integer
Set REC = New Recordset
Dim bit1() As Byte
Dim sa As String
sSql = "SELECT * FROM 表 WHERE 关键字 ='" & 关键值 & "'"
REC.Open sSql, Conn, adOpenStatic, adLockOptimistic, adCmdText
If REC.EOF Or REC.BOF Then
Exit Sub
Else
Picture1.Picture = Nothing
If REC("图片").ActualSize > 0 Then
bit1 = REC.Fields("图片").GetChunk(REC("图片").ActualSize)
'然后将字节数组的内容拼装成文件即可
Open "c:\1.bmp" For Binary As #1
Put 1, 1, bit1
Close 1
Picture1.Picture = LoadPicture("c:\1.bmp")
kill ("c:\1.bmp")
End If
End If
rec.close
Set REC = Nothing
Exit Sub
Err:
MsgBox "读取图片出错!", OKOnly, "系统提示"
End Sub
但先要将图片的路径和文件名的值附给commondialog,以下是例子
Private Sub Image1_DblClick()
CommonDialog1.Filter = "图片文件|*.jpg;*.bmp;*.gif"
CommonDialog1.ShowOpen
If CommonDialog1.FileName <> "" Then
Image1.Picture = LoadPicture(CommonDialog1.FileName)
End If
End Sub
Public mstream As ADODB.Stream
public sub addimage()
Set mstream = New ADODB.Stream
mstream.Type = adTypeBinary
mstream.Open
mstream.LoadFromFile CommonDialog1.FileName
adoXXX.Recordset.Fields("image").Value = mstream.Read
mstream.Close
End sub
image为数据里的一个字段,字段类型为image