vb6 and mysql blobs
I've got a working application right now that retrieves files from a blob column in my mysql db and then saves it to a file on my harddrive, what I would like to do instead of saving the files to my harddrive is store each file into an array or variable or just basically anything else other then my harddrive. Suggests please and example code appreciated.
[361 byte] By [
jtabish] at [2007-11-11 10:06:17]

# 4 Re: vb6 and mysql blobs
Why not use the ADO Streams object to load and save your files to and from your database. If you use Access as your database then the datatype for the field in your table would be an OLE_OBJECT. What I do is loop through my recordset and call one of two functions. The first is used to store a binary file in my field and the other is used to save the field back to a file. Fore an example here are to functions I use for storing image files; but you could store any kind of file you want. If you store more than one type of file in the same field the you will need to modify these functions to aslo store the file type as well. Which means you will need an additional field in your table that holds the file type. Here are the functions:'Store Image File into a field of a recordset
Public Function ReadFromFile(rs As ADODB.Recordset, str_full_path As String)
On Error GoTo Er
Dim fds As ADODB.Stream
ReadFromFile = False
Set fds = New ADODB.Stream
'Make it a binary type
fds.Type = adTypeBinary
'Open the stream
fds.Open
'
'*** Read the binary file into the stream buffer ***
'
fds.LoadFromFile str_full_path
' save binary data into Field of current record
rs!OLE_FIELD = fds.Read
rs.Update
ReadFromFile = True
Done:
fds.Close
Set fds = Nothing
Exit Function
Er:
Select Case Err.Number
Case 3002
MsgBox "Could not read file (" & str_full_path & ") , check the path or the file may be in use."
Case Else:
'Unexpected, fail with message box
MsgBox "Error # " & Err.Number & "--" & Error, vbCritical, "ReadFromFile()"
End Select
Resume Done
End Function
'Retreive Image From Field and save to file
Public Function WriteToFile(rs As ADODB.Recordset, str_full_path As String) As Boolean
On Error GoTo Er
Dim fds As ADODB.Stream
WriteToFile = False
Set fds = New ADODB.Stream
fds.Type = adTypeBinary
fds.Open
'get data from field in current record
fds.Write rs!OLE_FIELD
fds.SaveToFile str_full_path, adSaveCreateOverWrite
WriteToFile = True
Done:
fds.Close
Set fds = Nothing
Exit Function
Er:
Select Case Err.Number
Case 3004: ' file is in use!
MsgBox "File is either already in use or it's file permissions are incorrect. Check the path or the file may be in use.", vbExclamation, "WriteToFile()"
Case Else:
'Unexpected, fail with message box
MsgBox "Error # " & Err.Number & "--" & Error, vbCritical, "WriteToFile()"
End Select
Resume Done
End Function
# 6 Re: vb6 and mysql blobs
Wow, for some reason when I read your post the first time I thought you were trying to store different types of files to your database. After re-reading it, I see now you just need to get the data into a byte array. The GetChunk method is the only way I know of to do that. Here is a simple example:' Copy a binary data field's contents to a byte array.
Function GetBytes(fld As ADODB.Field) As Variant
Dim bytes As Long
Dim tmp() As Byte
' Raise an error if the field doesn't support GetChunk.
If (fld.Attributes And adFldLong) = 0 Then
Err.Raise 1001, , "Field doesn't support the GetChunk method."
End If
' Read the field's contents
bytes = fld.ActualSize
tmp = fld.GetChunk(bytes)
GetBytes = tmp()
Erase tmp
End Function