Dim Chunk() As Byte
Chunk() = Image2Chunk(Filename)
.Fields("thumb").AppendChunk Chunk()
.Update
Private Function Image2Chunk(Filename As String) As Variant
On Error GoTo ProcErr
Dim Datafile As Integer
Dim FileLength As Long
Dim Chunk() As Byte
Datafile = FreeFile
Open Filename For Binary Access Read As Datafile
FileLength = LOF(Datafile)
If FileLength = 0 Then GoTo ProcErr
ReDim Chunk(FileLength)
Get Datafile, , Chunk()
Close Datafile
存入ACCESS数据库中:
Dim rs As New ADODB.Recordset
Dim Rss As New ADODB.Stream
Dim cnn As ADODB.Connection
Dim cnstr As String
cnstr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + App.Path + "\pic.mdb;Mode=ReadWrite;Persist Security Info=False"
Set cnn = New ADODB.Connection
cnn.Open cnstr
rs.Open "test", cnn, adOpenStatic, adLockOptimistic
Rss.Type = adTypeBinary
Rss.Open
Rss.LoadFromFile App.Path & "\1.jpg"
rs.AddNew
rs.Fields("a1") = "PICTURE"
rs.Fields("a2") = Rss.Read
rs.Update
rs.Close
cnn.Close
a2的类型为OLE
读取:
Dim rs As New ADODB.Recordset
Dim Rss As New ADODB.Stream
Dim cnn As ADODB.Connection
Dim cnstr As String
cnstr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + App.Path + "\pic.mdb;Mode=ReadWrite;Persist Security Info=False"
Set cnn = New ADODB.Connection
cnn.Open cnstr
rs.Open "test", cnn, adOpenStatic, adLockOptimistic
Rss.Type = adTypeBinary
Rss.Open
If Not rs.EOF Then
Rss.Write rs.Fields("a2")
Rss.SaveToFile App.Path & "\tmp.jpg" ‘存为硬盘文件
Picture1.Picture = LoadPicture(App.Path & "\tmp.jpg")
Kill App.Path & "\tmp.jpg"
End If
rs.Close
cnn.Close
如果要存别的类型,用类似的方法。
'Use ADODB.Stream Method
'After ADO 2.6
'Import the Image to SQLServer
Private Sub ImportBLOB(cn As ADODB.Connection)
Dim rs As New ADODB.Recordset
Dim stm As ADODB.Stream
Set stm = New ADODB.Stream
' Skip any table not found errors
On Error Resume Next
cn.Execute "drop table BinaryObject"
On Error GoTo 0
'Create the BinaryObject table
cn.Execute "create table BinaryObject " & _
"(blob_id int IDENTITY(1,1), " & _
"blob_filename varchar(256), " & _
"blob_object image)"
rs.Open "Select * from BinaryObject where 1=2", cn, adOpenKeyset, adLockOptimistic
'Read the binary files from disk
stm.Type = adTypeBinary
stm.Open
stm.LoadFromFile App.Path & "\BLOBsample.jpg"
End Sub
'Display the image on image control
Private Sub DisplayBLOB(cn As ADODB.Connection)
Dim rs As New ADODB.Recordset
' Select the only image in the table
rs.Open "Select * from BinaryObject where blob_id = 1", cn
' Set the DataSource to the recordset
Set imgBinaryData.DataSource = rs
'Set the DataField to the BLOB field
imgBinaryData.DataField = rs!blob_object.Name