Click to See Complete Forum and Search --> : how to store file(any file) in database


miriyalasrinu
October 18th, 2001, 07:05 AM
How to store and retrieve a file in/from database.the file may be any type.for example gif file,word file & etc.

John G Duffy
October 18th, 2001, 09:59 AM
Look at the GetChunk and AppendChunk methods. Use them to split a file into large chunks for storage in a DataBase.

John G

michi
October 18th, 2001, 10:36 AM
If you user ADO in VB, you can use ADODB.Stream. I have a similar function here used for displaying all kinds of picture or even PDF file read from DB. Hope it can give you some hints.

=====
Private Function LoadImage() As Boolean
On Error GoTo LoadImage_Error
Const STR_LOAD_DIRECTORY As String = "\Save Picture"
Dim strSQL As String 'The SQL Command to execute
Dim i As Integer

Dim strPath As String
Dim strFileName As String
Dim strFullyQualifiedFileName As String

Dim rsRecord As ADODB.Recordset
Dim stmStream As ADODB.Stream

Dim blnContinue As Boolean

gclsEventLog.WriteStartDebug (Me.Name & ".LoadImage")

blnContinue = True

'
' Create objects
'
Set rsRecord = New ADODB.Recordset
Set stmStream = New ADODB.Stream

'
'To do: Build up SQL string (Omit)
'

'
'Open recordset to load in the database record
'

Call rsRecord.Open(strSQL, gadoWFIMSConn, adOpenKeyset, adLockPessimistic)

'
' Process record if successfully loaded.
'

If Not rsRecord.EOF Then
'
' Load image to Stream
'
stmStream.Type = adTypeBinary
stmStream.Open
stmStream.Write rsRecord.Fields("Image").Value

'
'Prepare the path and file name
'
strPath = App.Path & STR_LOAD_DIRECTORY
If Dir(strPath, vbDirectory) = "" Then
'
'If the directory does exist, create it.
'Note: CreateNewDirectory is another function in my project, omit here.
'
blnContinue = CreateNewDirectory(strPath)

End If

If blnContinue Then

'
'Temporarily copy the image from DB to local hard disk
'
strFileName = "My Temp File"
strFullyQualifiedFileName = strPath & "\" & strFileName
stmStream.SaveToFile strFullyQualifiedFileName, adSaveCreateOverWrite

'
'To Do: Show image (Omit)
'

'
'Delete this picture from local hard disk
'
Kill strFullyQualifiedFileName


'
'Clear memory
'
stmStream.Close
Set stmStream = Nothing


End If

End If

'
'Clear memory
'
rsRecord.Close
Set rsRecord = Nothing

LoadImage = True

gclsEventLog.WriteEndDebug

Exit Function

LoadImage_Error:

If (rsRecord.State = adStateOpen) Then
rsRecord.Close
End If
Set rsRecord = Nothing

If (stmStream.State = adStateOpen) Then
stmStream.Close
End If
Set stmStream = Nothing

End Function
==========

HTH

Regards,

Michi