CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Jul 2001
    Posts
    22

    how to store file(any file) in database

    How to store and retrieve a file in/from database.the file may be any type.for example gif file,word file & etc.



  2. #2
    Join Date
    Apr 2000
    Location
    South Carolina,USA
    Posts
    2,210

    Re: how to store file(any file) in database

    Look at the GetChunk and AppendChunk methods. Use them to split a file into large chunks for storage in a DataBase.

    John G

  3. #3
    Join Date
    May 2001
    Location
    Canada
    Posts
    182

    Re: how to store file(any file) in database

    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured