'***********************************************
Dim Conn As New ADODB.Connection
Dim FileName As String
Const BLOCKSIZE = 4096
Dim ADORst As New ADODB.Recordset 'ADODB Recordset
Dim ADOFld As ADODB.Field
Private Sub Command1_Click()
Save_Click
Dim arsfile As ADODB.Recordset
Dim aa As ADODB.Record
Dim n As Long
Dim arrBytes() As Byte
Set arsfile = New Recordset
arsfile.Open "select * from A_file where id=2", Conn, adOpenStatic, adLockOptimistic
n = arsfile.Fields("word").ActualSize
ReDim arrBytes(1 To n) As Byte
arrBytes = arsfile.Fields("PIC").GetChunk(n)
Open App.Path & "\test2.jpg" For Binary As #1
Put #1, , arrBytes
Close #1
End Sub
Private Sub Form_Load()
Set Conn = New Connection
Conn.CursorLocation = adUseClient
Conn.Provider = "MSDataShape"
Conn.Open "driver={sql server};server=10.0.0.1;uid=sa;pwd=;database=cmstest;"
ADORst.Open "A_File", Conn, adOpenDynamic, adLockOptimistic
'Set ADOFld.Recordset = ADORst '
End Sub
Private Sub SaveToDB(ByRef Fld As ADODB.Field, DiskFile As String)
Dim byteData() As Byte
Dim NumBlocks As Long
Dim FileLength As Long
Dim LeftOver As Long
Dim SourceFile As Long
Dim i As Long
SourceFile = FreeFile
Open App.Path & "\test.jpg" For Binary Access Read As SourceFile
FileLength = LOF(SourceFile)
If FileLength = 0 Then
Close SourceFile
MsgBox DiskFile
Else
NumBlocks = FileLength \ BLOCKSIZE
LeftOver = FileLength Mod BLOCKSIZE
Fld.Value = Null
ReDim byteData(BLOCKSIZE)
For i = 1 To NumBlocks
Get SourceFile, , byteData()
Fld.AppendChunk byteData()
Next i
ReDim byteData(LeftOver)
Get SourceFile, , byteData()
Fld.AppendChunk byteData()
Close SourceFile
End If
End Sub
Private Sub Save_Click()
ADORst.AddNew
ADORst("id").Value = 2
Set ADOFld = ADORst("PIC")
Call SaveToDB(ADOFld, FileName)