'追加图片到数据库
Public Function savebmptodb(ByVal strdh As String, ByVal ypbh As Long, ByVal sfilename As String) As Boolean
On Error GoTo err
savebmptodb = False
Dim strStream As New ADODB.Stream
Dim rs As New ADODB.Recordset
strStream.Type = adTypeBinary
strStream.Open
strStream.LoadFromFile sfilename
Dim strsql As String
strsql = "select * from tb_images where strdh='" & strdh & "' and ypbh=" & ypbh
rs.Open strsql, g_pubcnn, adOpenStatic, adLockOptimistic
If rs.RecordCount <= 0 Then
rs.AddNew
End If
'** 引用 Microsoft ActiveX Data Objects 2.5 Library 及以上版本
‘2.5版本以下不支持Stream对象
Dim iConcstr As String
Dim iConc As ADODB.Connection
'保存文件到数据库中
Sub s_SaveFile()
Dim iStm As ADODB.Stream
Dim iRe As ADODB.Recordset
Dim iConcstr As String
'读取文件到内容
Set iStm = New ADODB.Stream
With iStm
.Type = adTypeBinary '二进制模式
.Open
.LoadFromFile App.Path + "\test.jpg"
End With
'打开保存文件的表
Set iRe = New ADODB.Recordset
With iRe
.Open "select * from img", iConc, 1, 3
.AddNew '新增一条记录
.Fields("photo") = iStm.Read
.Update
End With
'完成后关闭对象
iRe.Close
iStm.Close
End Sub
Sub s_ReadFile()
Dim iStm As ADODB.Stream
Dim iRe As ADODB.Recordset
'打开表
Set iRe = New ADODB.Recordset
‘得到最新添加的纪录
iRe.Open "select top 1 * from img order by id desc", iConc, adOpenKeyset, adLockReadOnly
'保存到文件
Set iStm = New ADODB.Stream
With iStm
.Mode = adModeReadWrite
.Type = adTypeBinary
.Open
.Write iRe("photo")
‘这里注意了,如果当前目录下存在test1.jpg,会报一个文件写入失败的错误.
.SaveToFile App.Path & "\test1.jpg"
End With
Image1.Picture = LoadPicture(App.Path & "\test1.jpg")
'关闭对象
iRe.Close
iStm.Close
End Sub
Private Sub Command1_Click()
Call s_ReadFile
End Sub
Private Sub Command2_Click()
Call s_SaveFile
End Sub
转贴(摘自www.china-askpro.com):
注:写图片文件到数据库
Col为栏位名,ImgFile为要写到数据库的图片文件名,BockSize为每次写多少字节,缺省为每次写8K字节到数据库
Public Sub WriteDB(Col As ADODB.Field, ImgFile As String, Optional BlockSize As Long=8192)
Dim byteData() As Byte, FileLength As Long, NumBlocks As Integer
Dim LeftOver As Long, SourceFileNum As Integer, i As Integer
SourceFileNum = FreeFile
Open ImgFile For Binary As SourceFileNum
FileLength = LOF(SourceFileNum)
If FileLength > 50 Then
NumBlocks = FileLength \ BlockSize
LeftOver = FileLength Mod BlockSize
ReDim byteData(LeftOver)
Get SourceFileNum, , byteData()
Col.AppendChunk byteData()
ReDim byteData(BlockSize)
For i = 1 To NumBlocks
Get SourceFileNum, , byteData()
Col.AppendChunk byteData()
Next
End If
Close SourceFileNum
End Sub
ImgFile为从数据库读出数据写到磁盘的文件名,BlockSize为每次向文件写多少个字节,缺省为8K字节,当ReadDB=True,得到图片文件後,可以用LoadPicter(图片文件名)显示图片到PictureBox或Image框中.
Public Function ReadDB(Col As ADODB.Field, ImgFile As String,Optional BlockSize As Long=8192) As Boolean
Dim byteData() As Byte, NumBlocks As Integer
Dim LeftOver As Long, DestFileNum As Integer, i As Integer
Dim ColSize As Long
On Error GoTo ErrRead
ReadDB = False
'If Dir(ImgFile) <> "" Then Kill ImgFile
DestFileNum = FreeFile
Open ImgFile For Binary As #DestFileNum
ReDim byteData(LeftOver)
byteData() = Col.GetChunk(LeftOver)
Put DestFileNum, , byteData()
ReDim byteData(BlockSize)
For i = 1 To NumBlocks
byteData() = Col.GetChunk(BlockSize)
Put #DestFileNum, , byteData()
Next
If LOF(DestFileNum) > 200 Then ReadDB = True
Close #DestFileNum
Exit Function
ErrRead:
MsgBox "READ PICTURE ERR:" & Err.Number
ReadDB = False
Exit Function
End Function//如果ReadDB=False则写文件失败。
我这里有一个ACCESS的例子,你参考参考吧
Option Explicit
Dim b As ADODB.Recordset
Dim c As ADODB.Stream
Dim conn As New ADODB.Connection
Private Sub Command1_Click()
conn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\1.mdb;Persist Security Info=False"
conn.Execute "create table a (Str longbinary)"
End Sub
Private Sub Command2_Click()
Set b = New ADODB.Recordset
Set c = New ADODB.Stream
c.Mode = adModeReadWrite
c.Type = adTypeBinary
c.Open
c.LoadFromFile "D:\ss.JPG"
b.Open "select * from a", "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\1.mdb;Persist Security Info=False", adOpenDynamic, adLockOptimistic
b.AddNew
b.Fields.Item(0).Value = c.Read()
b.Update
b.Close
Set b = New ADODB.Recordset
b.Open "select * from a", "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:1.mdb;Persist Security Info=False", adOpenKeyset, adLockOptimistic
MsgBox b.RecordCount
b.MoveLast
c.Write (b.Fields.Item(0).Value)
c.SaveToFile "d:\aa.JPG", adSaveCreateOverWrite
Picture1.Picture = LoadPicture("D:\aa.JPG")
End Sub