存:
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
在工程菜单上,单击选择引用,然后设置一个到 Microsoft ActiveX Data Objects 2.5 Object Library 的引用。
将 CommandButton 控件放在 Form1 上。
在该窗体的常规声明段中作如下声明:
Dim cn As ADODB.Connection
Dim rs As ADODB.Recordset
Dim mstream As ADODB.Stream
将下面的代码剪切并粘贴到添加到该窗体的 CommandButton 所对应的 Click 事件中:
Set cn = New ADODB.Connection
cn.Open "Provider=SQLOLEDB;data Source=<name of your SQL Server>;
Initial Catalog=pubs;User Id=<Your Userid>;Password=<Your Password>"
Set rs = New ADODB.Recordset
rs.Open "Select * from pub_info", cn, adOpenKeyset, adLockOptimistic
Set mstream = New ADODB.Stream
mstream.Type = adTypeBinary
mstream.Open
mstream.Write rs.Fields("logo").Value
mstream.SaveToFile "c:\publogo.gif", adSaveCreateOverWrite
rs.Close
cn.Close
保存并运行该 Visual Basic 工程。
单击 CommandButton,即可将第一条记录的 logo 列所包含的二进制数据保存到文件 c:\publogo.gid 中。请用资源管理器找到并打开该文件,以查看所保存的图像。
在工程菜单上,单击选择引用,然后设置一个到 Microsoft ActiveX Data Objects 2.5 Object Library 的引用。
将 CommandButton 按钮放在 Form1 上。
在该窗体的常规声明段中作如下声明:
Dim cn As ADODB.Connection
Dim rs As ADODB.Recordset
Dim mstream As ADODB.Stream
将下面的代码剪切并粘贴到添加到该窗体的 CommandButton 所对应的 Click 事件中:
Set cn = New ADODB.Connection
cn.Open "Provider=SQLOLEDB;data Source=<name of your SQL Server>;
Initial Catalog=pubs;User Id=<Your Userid>;Password=<Your Password>"
Set rs = New ADODB.Recordset
rs.Open "Select * from pub_info", cn, adOpenKeyset, adLockOptimistic
Set mstream = New ADODB.Stream
mstream.Type = adTypeBinary
mstream.Open
mstream.LoadFromFile "<path to .gif file>"
rs.Fields("logo").Value = mstream.Read
rs.Update
'二进制数据转换为文件形式
'sfilename为一临时文件名。
Public Function BinarytoFile(sFileName As String, fld As ADODB.Field) As Variant
Dim bBuffer() As Byte
Dim nLenLeft As Long
Dim nChunkSize As Long
Dim FileNO As Long
If Len(Dir$(sFileName)) > 0 Then
Close
Kill sFileName
End If
FileNO = FreeFile
Open sFileName For Binary As #FileNO
nChunkSize = 32768
nLenLeft = fld.ActualSize
If nLenLeft = 0 Then
BinarytoFile = Empty
Exit Function
End If
If nLenLeft < nChunkSize Then
nChunkSize = nLenLeft
End If
Do
ReDim bBuffer(nChunkSize - 1)
bBuffer = fld.GetChunk(nChunkSize)
Put #FileNO, , bBuffer
nLenLeft = nLenLeft - nChunkSize
If nLenLeft < nChunkSize Then
nChunkSize = nLenLeft
End If
Loop Until nLenLeft <= 0
Close #FileNO
BinarytoFile = sFileName
End Function
'图形转换成二进制文件。
Public Function FileIoBinary(strfile As String) As Variant
If strfile = "" Then
FileIoBinary = Empty
Exit Function
End If
Open strfile For Binary As #1
ReDim Binfile(FileLen(strfile) - 1)
Get #1, , Binfile
Close #1
FileIoBinary = Binfile
' MsgBox UBound(Binfile)
End Function
'例如:
Image1.Picture = LoadPicture(IIf(IsEmpty(Rsgz("photo")), Empty, BinarytoFile("bmptemp", Rsgz("photo"))))
数据库中“photo”为存储图像字段,好象是image类型。
添加时采用addnew 或
RsPhoto.Fields("photo").AppendChunk FTPhoto
FTPhoto为二进制数据文件。
Private Sub SavePhoto(ByRef fld As ADODB.Field, DiskFile As String)
Dim byteData() As Byte '定义数据块数组
Dim NumBlocks As Long '定义数据块个数
Dim FileLength As Long '标识文件长度
Dim LeftOver As Long '定义剩余字节长度
Dim SourceFile As Long '定义自由文件号
Dim i As Long '定义循环变量
SourceFile = FreeFile '提供一个尚未使用的文件号
Open DiskFile For Binary Access Read As SourceFile '打开文件
FileLength = LOF(SourceFile) '得到文件长度
If FileLength = 0 Then '判断文件是否存在
Close SourceFile
MsgBox DiskFile & " 无内容或不存在!"
Else
NumBlocks = FileLength \ BLOCKSIZE '得到数据块的个数
LeftOver = FileLength Mod BLOCKSIZE '得到剩余字节数
fld.Value = Null
ReDim byteData(BLOCKSIZE) '重新定义数据块的大小
For i = 1 To NumBlocks
Get SourceFile, , byteData() ' 读到内存块中
fld.AppendChunk byteData() '写入FLD
Next i
ReDim byteData(LeftOver) '重新定义数据块的大小
Get SourceFile, , byteData() '读到内存块中
fld.AppendChunk byteData() '写入FLD
Close SourceFile '关闭源文件
End If
End Sub