在ADO2.6中有一個流對象:好像是ADODB.Stream吧,它可以設置為一個文件,再對Recordset對象新增記錄時附值給image類型的段后就可以保存到數據庫中了。讀取文件時你可以看看如下一個讀取圖片的代碼: Function setPicture(ID As String) As Long '根据给出的ID号显示图片 'On Error GoTo err: Dim lngLogoSize As Long '图片大小用于定义数组下标 Dim varLogo() As Byte '数组,下标由lngLogoSize决定 Dim strFileNamePath As String '临时文件路径、文件名 Dim FileHandle As Long '临时文件文件号 Dim Retemp As New ADODB.Recordset Retemp.Open "select PID,Image from Product where PID='" & ID & "'", CnMain, adOpenStatic, adLockReadOnly, adCmdText If Retemp.RecordCount = 0 Then setPicture = 0 Exit Function End If strFileNamePath = "C:\temp.jpg" '得到临时文件路径文件名 FileHandle = FreeFile Open strFileNamePath For Binary As #FileHandle lngLogoSize = Retemp("image").ActualSize If lngLogoSize <= 0 Then setPicture = 0 Exit Function End If ReDim varLogo(lngLogoSize - 1) varLogo() = Retemp("image").GetChunk(lngLogoSize) Put #FileHandle, , varLogo Close #FileHandle Retemp.Close: Set Retemp = Nothing setPicture = 1 Exit Function 'err: ' MsgBox "出现错误!错误号" & CStr(err.Number) & "," & err.Description, vbOKOnly + vbExclamation ' setPicture = 0 End Function
我以前有源代码,不过不知道扔到那儿了,懒得找了:)
Function setPicture(ID As String) As Long '根据给出的ID号显示图片
'On Error GoTo err:
Dim lngLogoSize As Long '图片大小用于定义数组下标
Dim varLogo() As Byte '数组,下标由lngLogoSize决定
Dim strFileNamePath As String '临时文件路径、文件名
Dim FileHandle As Long '临时文件文件号
Dim Retemp As New ADODB.Recordset
Retemp.Open "select PID,Image from Product where PID='" & ID & "'", CnMain, adOpenStatic, adLockReadOnly, adCmdText
If Retemp.RecordCount = 0 Then
setPicture = 0
Exit Function
End If
strFileNamePath = "C:\temp.jpg" '得到临时文件路径文件名
FileHandle = FreeFile
Open strFileNamePath For Binary As #FileHandle
lngLogoSize = Retemp("image").ActualSize
If lngLogoSize <= 0 Then
setPicture = 0
Exit Function
End If
ReDim varLogo(lngLogoSize - 1)
varLogo() = Retemp("image").GetChunk(lngLogoSize)
Put #FileHandle, , varLogo
Close #FileHandle
Retemp.Close: Set Retemp = Nothing
setPicture = 1
Exit Function
'err:
' MsgBox "出现错误!错误号" & CStr(err.Number) & "," & err.Description, vbOKOnly + vbExclamation
' setPicture = 0
End Function