Click here to Skip to main content
Licence 
First Posted 30 Nov 1999
Views 152,220
Bookmarked 50 times

Using CLongBinary for BLOBs

By | 30 Nov 1999 | Article
 
Part of The SQL Zone sponsored by
See Also
  • Download demo project - 29 Kb
  • Sample Image - UsingBlob.gif

    At times, storing images and other types of documents like word document, or excel spread sheet or even a compressed zip file in the database is inevitable. MFC provides a supporting class to handle these BLOBs called CLongBinary. It is just a wrapper around the HGLOBAL of WIN32. But how do you use it? The wizard creates a member variable of type CLongBinary in your record set object and also creates the corresponding RFX routine (RFX_LongBinary).

    Okay, does it mean that you can use this member just like any other variable? No, That's where this document comes in handy. For regular data types, all you have to is create an instance of the recordset object, assign the values and call Update. But for BLOBs, you have to do something special. For inserting BLOBs, you have to remember 3 points.

    1. Allocate and lock the storage buffer using GlobalAlloc() and GlobalLock() calls
    2. Prepare the recordset for blob operation using SetFieldDirty() and SetFieldNull() calls
    3. Unlock the storage buffer

    By default, the destructor of the CLongBinary will delete the buffer using GlobalFree() call. So you don't have to bother about freeing the memory. Reading from the database is pretty simple. All you have to do is lock the buffer.

    Following code snippet shows how to do this in the actual code:

    // Store the Image to database table
    // 
    try
    {
        dbImages.Open();
        dbImages.AddNew();
    
        CFile	fileImage;
        CFileStatus	fileStatus;
    
        fileImage.Open("c:\\winnt\\winnt256.bmp", CFile::modeRead);
        fileImage.GetStatus(fileStatus);
    
        dbImages.m_BLOBName = fileImage.GetFileTitle();
        dbImages.m_BLOBImage.m_dwDataLength = fileStatus.m_size;
    
        HGLOBAL hGlobal		= GlobalAlloc(GPTR,fileStatus.m_size);
        dbImages.m_BLOBImage.m_hData = GlobalLock(hGlobal);
    
        fileImage.ReadHuge(dbImages.m_BLOBImage.m_hData,fileStatus.m_size);
    
        dbImages.SetFieldDirty(&dbImages.m_BLOBImage);
        dbImages.SetFieldNull(&dbImages.m_BLOBImage,FALSE);
        dbImages.Update();
    
        GlobalUnlock(hGlobal);
    
        dbImages.Close();
    
        pList->InsertItem(0,fileImage.GetFileTitle());
    }
    catch(CException* pE)
    {
        pE->ReportError();
        pE->Delete();
        return;
    }

     

    // To restore image from db table
    CdbImages   dbImages(&theApp.m_DB);
    CString     strFileName = pList->GetItemText(nIndex,0);
    dbImages.m_strFilter.Format("BLOBName = '%s'",strFileName);
    try
    {
        dbImages.Open();
        if  (dbImages.IsEOF())
            AfxMessageBox("Unable to get image from db");
        else
        {
            char    tmpPath[_MAX_PATH+1];
            GetTempPath(_MAX_PATH,tmpPath);
    
            strFileName.Insert(0,tmpPath);
            
            CFile	outFile(strFileName,CFile::modeCreate|CFile::modeWrite);
            LPSTR	buffer = (LPSTR)GlobalLock(dbImages.m_BLOBImage.m_hData);
            outFile.WriteHuge(buffer,dbImages.m_BLOBImage.m_dwDataLength);
            GlobalUnlock(dbImages.m_BLOBImage.m_hData);
            outFile.Close();
    
            theApp.OpenDocumentFile(strFileName);
        }
    
        dbImages.Close();
    
    }
    catch(CException* pE)
    {
        pE->ReportError();
        pE->Delete();
        return;
    }
    
    

    The demo project uses an Access database to store the images. This project also demonstrates the following:

    • DSN - less Connection to MS Access database
    • Dialog Bars
    • Image Display in CScrollView

    The images are displayed using the simplest technique of using BitBlt() call. It does not use any Palettes because it is beyond the scope of this article.

    License

    This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

    A list of licenses authors might use can be found here

    About the Author

    Shekar Narayanan



    United States United States

    Member



    Sign Up to vote   Poor Excellent
    Add a reason or comment to your vote: x
    Votes of 3 or less require a comment

    Comments and Discussions

     
    You must Sign In to use this message board. (secure sign-in)
     
    Search this forum  
     FAQ
        Noise  Layout  Per page   
      Refresh
    GeneralStore Images with RecordsetPtr Pinmemberjsundjsund21:57 7 Apr '03  
    GeneralOut of memory error with Sybase PinmemberStan Harris16:10 20 Feb '03  
    GeneralRe: Out of memory error with Sybase PinmemberWes Jones8:06 28 Jul '06  
    QuestionHow to Delete or Modify a BLOB? PinsussMate15:48 7 Jan '03  
    AnswerRe: How to Delete or Modify a BLOB? PinsussBen Mathews10:42 13 Feb '03  
    AnswerRe: How to Delete or Modify a BLOB? Pinmemberipadilla6:31 4 Sep '05  
    GeneralProblem while updating the BLOB to ORACLE using CLongBinary PinmemberMytreya3:14 25 Mar '02  
    I've been using OCI library calls till recently to talk
    to ORACLE D/B and it works pretty fine. But when i try
    to use ODBC for the same, i am not sure as how the
    BLOB object needs to be updated to the D/B. I've tried
    it as mentioned in this article, but encounter the error
    MissingExpression: ora-00936.
    Also when we use OCI calls, we select the OCILobLocator for
    update, write into it and just commit. But in the ODBC
    version, when i used the edit/update, i observed that
    the ultimate SQL statement that is being framed is an
    "update".
    If anyone has done the BLOB updation to ORACLE using
    ODBC, please do let me know as how it needs to be done.

    GeneralRe: Problem while updating the BLOB to ORACLE using CLongBinary PinmemberDiWa5:18 7 Aug '02  
    GeneralThanks for your excellent job,by the way,my specific solution Pinmemberseaguard20:54 9 Jan '02  
    GeneralI have Exception - "Data truncated." PinmemberAlexander Sirotkin1:15 23 Oct '01  
    GeneralLong Binary field !!! PinmemberHadi_Rezaie17:24 6 Oct '01  
    GeneralRe: Long Binary field !!! PinmemberAnonymous7:05 15 Jun '02  
    QuestionGlobalUnlock?? PinmemberScott Wood15:28 22 Dec '00  
    GeneralThanks! Pinsussscott wood6:58 10 Jul '00  
    QuestionWhy create temporary files? PinsussSteve Knoblock13:58 11 Jun '00  
    AnswerRe: Why create temporary files? Pinsussscott wood6:55 10 Jul '00  
    GeneralUsing CLongBinary with LONG (Oracle) PinsussIlango18:58 28 May '00  
    GeneralRe: Using CLongBinary with LONG (Oracle) PinsussHe Huiguang2:18 1 Jun '00  
    GeneralRe: Using CLongBinary with LONG (Oracle) PinsussHassan Zia0:18 12 Sep '00  
    GeneralRe: Using CLongBinary with LONG (Oracle) Pinmemberljtang17:11 3 Feb '01  
    GeneralRe: Using CLongBinary with LONG (Oracle) PinmemberAnonymous16:18 15 May '01  
    GeneralRe: Using CLongBinary with LONG (Oracle) PinmemberC Ortega6:34 21 Jan '02  
    GeneralRe: Using CLongBinary with LONG (Oracle) PinmemberAnonymous20:41 26 Mar '01  
    GeneralRe: Using CLongBinary with LONG (Oracle) PinmemberAnonymous18:05 2 Aug '01  
    GeneralI get "Binary Long Data" instead of "Bitmap" Pinsusskohlmaier5:09 14 Apr '04  

    General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

    Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

    Permalink | Advertise | Privacy | Mobile
    Web03 | 2.5.120529.1 | Last Updated 1 Dec 1999
    Article Copyright 1999 by Shekar Narayanan
    Everything else Copyright © CodeProject, 1999-2012
    Terms of Use
    Layout: fixed | fluid