3. VB把文件存入数据库IMAGE字段Sub savepic(FileName As String, IndexNumber As Long) Dim DcnNWind As New ADODB.Connection Dim rs As ADODB.Recordset Set rs = New ADODB.Recordset DcnNWind.CursorLocation = adUseClient DcnNWind.Open "Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=CUSTOM;Data Source=SERVER" rs.CursorType = adOpenKeyset rs.LockType = adLockOptimistic rs.Open "CustomInfo", DcnNWind, , adCmdTable rs.Move (IndexNumber) Call FileToBlob(rs.Fields("Image"), FileName, FileLen(FileName)) rs.UpdateBatch adAffectCurrent End SubPrivate Sub FileToBlob(fld As ADODB.Field, FileName As String, Optional ChunkSize As Long ) Dim fnum As Integer, bytesLeft As Long, bytes As Long Dim tmp() As Byte If (fld.Attributes And adFldLong) = 0 Then Err.Raise 1001, , "Field doesn't support the GetChunk method." End If fnum = FreeFile Open FileName For Binary As fnum bytesLeft = LOF(fnum) Do While bytesLeft bytes = bytesLeft If bytes > ChunkSize Then bytes = ChunkSize ReDim tmp(1 To bytes) As Byte Get #1, , tmp fld.AppendChunk tmp bytesLeft = bytesLeft - bytes Loop Close #fnum End Sub4. VB把文件从IMAGE字段中读到文件中。Sub loadpic(IndexNumber As Long) Dim DcnNWind As New ADODB.Connection Dim rs As ADODB.Recordset Set rs = New ADODB.Recordset DcnNWind.CursorLocation = adUseClient DcnNWind.Open "Provider=SQLOLEDB.1;Integrated Security=SSI;Persist Security Info=False;Initial Catalog=CUSTOM;Data Source=SERVER" rs.CursorType = adOpenKeyset rs.LockType = adLockOptimistic rs.Open "CustomInfo", DcnNWind, , adCmdTable rs.Move (IndexNumber) Call BlobToFile(rs.Fields("Image"), "c:windows emp mp.bmp", rs.Fields("Image").ActualSize) End SubPrivate Sub BlobToFile(fld As ADODB.Field, FileName As String, Optional ChunkSize As Long ) Dim fnum As Integer, bytesLeft As Long, bytes As Long Dim tmp() As Byte If (fld.Attributes And adFldLong) = 0 Then Err.Raise 1001, , "Field doesn't support the GetChunk method." End If If Dir$(FileName) <> "" Then Kill FileName fnum = FreeFile Open FileName For Binary As fnum bytesLeft = fld.ActualSize Do While bytesLeft bytes = bytesLeft If bytes > ChunkSize Then bytes = ChunkSize tmp = fld.GetChunk(bytes) Put #fnum, , tmp bytesLeft = bytesLeft - bytes Loop Close #fnum End Sub
Dim DcnNWind As New ADODB.Connection
Dim rs As ADODB.Recordset
Set rs = New ADODB.Recordset
DcnNWind.CursorLocation = adUseClient
DcnNWind.Open "Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=CUSTOM;Data Source=SERVER"
rs.CursorType = adOpenKeyset
rs.LockType = adLockOptimistic
rs.Open "CustomInfo", DcnNWind, , adCmdTable
rs.Move (IndexNumber)
Call FileToBlob(rs.Fields("Image"), FileName, FileLen(FileName))
rs.UpdateBatch adAffectCurrent
End SubPrivate Sub FileToBlob(fld As ADODB.Field, FileName As String, Optional ChunkSize As Long )
Dim fnum As Integer, bytesLeft As Long, bytes As Long
Dim tmp() As Byte
If (fld.Attributes And adFldLong) = 0 Then
Err.Raise 1001, , "Field doesn't support the GetChunk method."
End If
fnum = FreeFile
Open FileName For Binary As fnum
bytesLeft = LOF(fnum)
Do While bytesLeft
bytes = bytesLeft
If bytes > ChunkSize Then bytes = ChunkSize
ReDim tmp(1 To bytes) As Byte
Get #1, , tmp
fld.AppendChunk tmp
bytesLeft = bytesLeft - bytes
Loop
Close #fnum
End Sub4. VB把文件从IMAGE字段中读到文件中。Sub loadpic(IndexNumber As Long)
Dim DcnNWind As New ADODB.Connection
Dim rs As ADODB.Recordset
Set rs = New ADODB.Recordset
DcnNWind.CursorLocation = adUseClient
DcnNWind.Open "Provider=SQLOLEDB.1;Integrated Security=SSI;Persist Security Info=False;Initial Catalog=CUSTOM;Data Source=SERVER"
rs.CursorType = adOpenKeyset
rs.LockType = adLockOptimistic
rs.Open "CustomInfo", DcnNWind, , adCmdTable
rs.Move (IndexNumber)
Call BlobToFile(rs.Fields("Image"), "c:windows emp mp.bmp", rs.Fields("Image").ActualSize)
End SubPrivate Sub BlobToFile(fld As ADODB.Field, FileName As String, Optional ChunkSize As Long )
Dim fnum As Integer, bytesLeft As Long, bytes As Long
Dim tmp() As Byte
If (fld.Attributes And adFldLong) = 0 Then
Err.Raise 1001, , "Field doesn't support the GetChunk method."
End If
If Dir$(FileName) <> "" Then Kill FileName
fnum = FreeFile
Open FileName For Binary As fnum
bytesLeft = fld.ActualSize
Do While bytesLeft
bytes = bytesLeft
If bytes > ChunkSize Then bytes = ChunkSize
tmp = fld.GetChunk(bytes)
Put #fnum, , tmp
bytesLeft = bytesLeft - bytes
Loop
Close #fnum
End Sub