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 '打开的文件号 你可以直接写 例如: 1 Open ImgFile For Binary As #SourceFileNum 'ImgFile是图片所在目录 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
'******************将图片文件保存到数据库中************************* Sub SavePicToDb(cn As ADODB.Connection, table1 As String, field1 As String, file1 As String, id1 As String) On Error Resume Next Dim stm As ADODB.Stream Set stm = New ADODB.Stream Set rs1 = New ADODB.Recordset rs1.Open "select * from " & table1 & " where id = " & id1, cn, adOpenKeyset, adLockOptimistic With stm .Type = adTypeBinary .Open .LoadFromFile file1 'DLG.FileName End With With rs1 .Fields(field1) = stm.Read .Update End With rs1.Close Set rs1 = Nothing End Sub
'**************************************************************** Sub GetPicFromDB(cn As ADODB.Connection) On Error Resume Next Dim fld As Field Dim strTemp As String Dim stm As ADODB.Stream Set stm = New ADODB.Stream 'strTemp = "c:\temp.bmp" Set rs1 = New ADODB.Recordset rs1.Open "select * from rs_http where htbh='" & frm_manage.Grid2.TextMatrix(frm_manage.Grid2.RowSel, 0) & "'", cn, , , adCmdText While Not rs1.EOF '*********将数据库中的文件读到硬盘上************************* ' strTemp = App.Path + "\temp\" + rs1!Name '`临时文件,用来保存读出的图片 With stm .Type = adTypeBinary .Open .Write rs1("tp").value strTemp = App.Path & "\temp1\" & rs1!Name .SaveToFile strTemp, adSaveCreateOverWrite .Close End With Set itemX = lvwPic.ListItems.add(, App.Path & "\temp1\" & rs1!Name, rs1!Name, 1, 1) itemX.SubItems(1) = rs1!bz rs1.MoveNext Wend Set stm = Nothing rs1.Close Set rs1 = Nothing End Sub
Dim byteData() As Byte, FileLength As Long, NumBlocks As Integer
Dim LeftOver As Long, SourceFileNum As Integer, i As Integer
SourceFileNum = FreeFile '打开的文件号 你可以直接写 例如: 1
Open ImgFile For Binary As #SourceFileNum 'ImgFile是图片所在目录
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
http://support.microsoft.com/default.aspx?scid=kb;EN-US;258038
Sub SavePicToDb(cn As ADODB.Connection, table1 As String, field1 As String, file1 As String, id1 As String)
On Error Resume Next
Dim stm As ADODB.Stream
Set stm = New ADODB.Stream
Set rs1 = New ADODB.Recordset
rs1.Open "select * from " & table1 & " where id = " & id1, cn, adOpenKeyset, adLockOptimistic
With stm
.Type = adTypeBinary
.Open
.LoadFromFile file1 'DLG.FileName
End With
With rs1
.Fields(field1) = stm.Read
.Update
End With
rs1.Close
Set rs1 = Nothing
End Sub
Sub GetPicFromDB(cn As ADODB.Connection)
On Error Resume Next
Dim fld As Field
Dim strTemp As String
Dim stm As ADODB.Stream
Set stm = New ADODB.Stream
'strTemp = "c:\temp.bmp"
Set rs1 = New ADODB.Recordset
rs1.Open "select * from rs_http where htbh='" & frm_manage.Grid2.TextMatrix(frm_manage.Grid2.RowSel, 0) & "'", cn, , , adCmdText
While Not rs1.EOF
'*********将数据库中的文件读到硬盘上*************************
' strTemp = App.Path + "\temp\" + rs1!Name '`临时文件,用来保存读出的图片 With stm
.Type = adTypeBinary
.Open
.Write rs1("tp").value
strTemp = App.Path & "\temp1\" & rs1!Name
.SaveToFile strTemp, adSaveCreateOverWrite
.Close
End With
Set itemX = lvwPic.ListItems.add(, App.Path & "\temp1\" & rs1!Name, rs1!Name, 1, 1)
itemX.SubItems(1) = rs1!bz
rs1.MoveNext
Wend
Set stm = Nothing
rs1.Close
Set rs1 = Nothing
End Sub