Click here to Skip to main content
11,495,486 members (67,255 online)
Click here to Skip to main content

Using CLongBinary for BLOBs

, 30 Nov 1999 168.2K 2K 51
Rate this:
Please Sign up or sign in to vote.
  • Download demo project - 29 Kb
  • Sample Image - UsingBlob.gif

    <!-- Article Starts -->

    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

    Share

    About the Author

    Shekar Narayanan

    United States United States
    No Biography provided

    Comments and Discussions

     
    GeneralMy vote of 5 Pin
    Member 41790757-Aug-11 11:56
    memberMember 41790757-Aug-11 11:56 
    QuestiondbImages.Open() is taking 100% CPU usage?? Pin
    Gokulnath00715-Jul-11 2:15
    memberGokulnath00715-Jul-11 2:15 
    QuestionDynamic Table name Pin
    Member 799544422-Jun-11 19:05
    memberMember 799544422-Jun-11 19:05 
    GeneralProblem with Recordset.Open(CRecordset::snapshot,_T("SELECT * FROM TEST")) function Pin
    navneet.professional5-May-10 1:50
    membernavneet.professional5-May-10 1:50 
    Generalerror C2039: 'WriteHuge' : is not a member of 'CFile' g:\mainfrm.cpp and Error and error C2039: 'ReadHuge' : is not a member of 'CFile' g:\mainfrm.cpp 205 Pin
    KYAW KYAW OO31-Jul-08 18:49
    memberKYAW KYAW OO31-Jul-08 18:49 
    GeneralRe: error C2039: 'WriteHuge' : is not a member of 'CFile' g:\mainfrm.cpp and Error and error C2039: 'ReadHuge' : is not a member of 'CFile' g:\mainfrm.cpp 205 Pin
    Rolando E. Cruz-Marshall26-Sep-08 3:04
    memberRolando E. Cruz-Marshall26-Sep-08 3:04 
    GeneralReally Good Pin
    manish rastogi3-Jul-08 0:48
    membermanish rastogi3-Jul-08 0:48 
    Generalsome question about insert images,please tell me! Pin
    freesky32325-Jan-07 4:03
    memberfreesky32325-Jan-07 4:03 
    Questionnew record Pin
    ali212121212121219-May-06 5:36
    memberali212121212121219-May-06 5:36 
    Questionhow to insert image to SQL server Pin
    thanhthuyvn_vtajkhskjakjd12-Nov-05 21:18
    memberthanhthuyvn_vtajkhskjakjd12-Nov-05 21:18 
    GeneralBLOB Pin
    pmk_820-Jun-05 2:04
    memberpmk_820-Jun-05 2:04 
    GeneralAn annotation Pin
    buho_usp7-Jun-05 5:17
    memberbuho_usp7-Jun-05 5:17 
    GeneralODBC Crash with CLongBinary Pin
    Anonymous21-Oct-04 23:53
    sussAnonymous21-Oct-04 23:53 
    GeneralODBC Crash with CLongBinary Pin
    Anonymous21-Oct-04 23:34
    sussAnonymous21-Oct-04 23:34 
    Generaldo the method work when considering oracle Pin
    xuyang_deng21-Apr-04 22:28
    memberxuyang_deng21-Apr-04 22:28 
    GeneralCByteArray Pin
    mjwilliamson16-Mar-04 2:16
    membermjwilliamson16-Mar-04 2:16 
    GeneralExcel files Pin
    Al Findlay17-Feb-04 8:12
    memberAl Findlay17-Feb-04 8:12 
    GeneralError: Data truncated Pin
    mef5263-Jul-03 7:06
    membermef5263-Jul-03 7:06 
    GeneralRe: Error: Data truncated Pin
    wancol25-Aug-03 22:59
    memberwancol25-Aug-03 22:59 
    Generalproblem when call update() Pin
    CS4291-Jul-03 22:24
    memberCS4291-Jul-03 22:24 
    QuestionCan u give an example with ADO about how to save a CLongBinary into database? Pin
    ztliu014-Jun-03 23:22
    memberztliu014-Jun-03 23:22 
    GeneralStore Images with RecordsetPtr Pin
    jsundjsund7-Apr-03 22:57
    memberjsundjsund7-Apr-03 22:57 
    GeneralOut of memory error with Sybase Pin
    Stan Harris20-Feb-03 17:10
    memberStan Harris20-Feb-03 17:10 
    GeneralRe: Out of memory error with Sybase Pin
    Wes Jones28-Jul-06 9:06
    memberWes Jones28-Jul-06 9:06 
    QuestionHow to Delete or Modify a BLOB? Pin
    Mate7-Jan-03 16:48
    sussMate7-Jan-03 16:48 
    AnswerRe: How to Delete or Modify a BLOB? Pin
    Ben Mathews13-Feb-03 11:42
    sussBen Mathews13-Feb-03 11:42 
    AnswerRe: How to Delete or Modify a BLOB? Pin
    ipadilla4-Sep-05 7:31
    memberipadilla4-Sep-05 7:31 
    GeneralProblem while updating the BLOB to ORACLE using CLongBinary Pin
    Mytreya25-Mar-02 4:14
    memberMytreya25-Mar-02 4:14 
    GeneralRe: Problem while updating the BLOB to ORACLE using CLongBinary Pin
    DiWa7-Aug-02 6:18
    memberDiWa7-Aug-02 6:18 
    GeneralThanks for your excellent job,by the way,my specific solution Pin
    seaguard9-Jan-02 21:54
    memberseaguard9-Jan-02 21:54 
    GeneralI have Exception - "Data truncated." Pin
    Alexander Sirotkin23-Oct-01 2:15
    memberAlexander Sirotkin23-Oct-01 2:15 
    GeneralLong Binary field !!! Pin
    Hadi_Rezaie6-Oct-01 18:24
    memberHadi_Rezaie6-Oct-01 18:24 
    GeneralRe: Long Binary field !!! Pin
    Anonymous15-Jun-02 8:05
    memberAnonymous15-Jun-02 8:05 
    QuestionGlobalUnlock?? Pin
    Scott Wood22-Dec-00 16:28
    memberScott Wood22-Dec-00 16:28 
    GeneralThanks! Pin
    scott wood10-Jul-00 7:58
    sussscott wood10-Jul-00 7:58 
    QuestionWhy create temporary files? Pin
    Steve Knoblock11-Jun-00 14:58
    sussSteve Knoblock11-Jun-00 14:58 
    AnswerRe: Why create temporary files? Pin
    scott wood10-Jul-00 7:55
    sussscott wood10-Jul-00 7:55 
    GeneralUsing CLongBinary with LONG (Oracle) Pin
    Ilango28-May-00 19:58
    sussIlango28-May-00 19:58 
    GeneralRe: Using CLongBinary with LONG (Oracle) Pin
    He Huiguang1-Jun-00 3:18
    sussHe Huiguang1-Jun-00 3:18 
    GeneralRe: Using CLongBinary with LONG (Oracle) Pin
    Hassan Zia12-Sep-00 1:18
    sussHassan Zia12-Sep-00 1:18 
    GeneralRe: Using CLongBinary with LONG (Oracle) Pin
    ljtang3-Feb-01 18:11
    memberljtang3-Feb-01 18:11 
    GeneralRe: Using CLongBinary with LONG (Oracle) Pin
    Anonymous15-May-01 17:18
    memberAnonymous15-May-01 17:18 
    GeneralRe: Using CLongBinary with LONG (Oracle) Pin
    C Ortega21-Jan-02 7:34
    memberC Ortega21-Jan-02 7:34 
    GeneralRe: Using CLongBinary with LONG (Oracle) Pin
    Anonymous26-Mar-01 21:41
    memberAnonymous26-Mar-01 21:41 
    GeneralRe: Using CLongBinary with LONG (Oracle) Pin
    Anonymous2-Aug-01 19:05
    memberAnonymous2-Aug-01 19:05 

    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.

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