Skip to main content
Email Password   helpLost your password?
  • 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:

    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.

    You must Sign In to use this message board.
     
     
    Per page   
     FirstPrevNext
    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 OO
    18:49 31 Jul '08  
    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-Marshall
    3:04 26 Sep '08  
    GeneralReally Good Pin
    manish rastogi
    0:48 3 Jul '08  
    Generalsome question about insert images,please tell me! Pin
    freesky323
    4:03 25 Jan '07  
    Questionnew record Pin
    ali21212121212121
    5:36 9 May '06  
    Generalhow to insert image to SQL server Pin
    thanhthuyvn_vtajkhskjakjd
    21:18 12 Nov '05  
    GeneralBLOB Pin
    pmk_8
    2:04 20 Jun '05  
    GeneralAn annotation Pin
    buho_usp
    5:17 7 Jun '05  
    GeneralODBC Crash with CLongBinary Pin
    Anonymous
    23:53 21 Oct '04  
    GeneralODBC Crash with CLongBinary Pin
    Anonymous
    23:34 21 Oct '04  
    Generaldo the method work when considering oracle Pin
    xuyang_deng
    22:28 21 Apr '04  
    GeneralCByteArray Pin
    mjwilliamson
    2:16 16 Mar '04  
    GeneralExcel files Pin
    Al Findlay
    8:12 17 Feb '04  
    GeneralError: Data truncated Pin
    mef526
    7:06 3 Jul '03  
    GeneralRe: Error: Data truncated Pin
    wancol
    22:59 25 Aug '03  
    Generalproblem when call update() Pin
    CS429
    22:24 1 Jul '03  
    GeneralCan u give an example with ADO about how to save a CLongBinary into database? Pin
    ztliu01
    23:22 4 Jun '03  
    GeneralStore Images with RecordsetPtr Pin
    jsundjsund
    22:57 7 Apr '03  
    GeneralOut of memory error with Sybase Pin
    Stan Harris
    17:10 20 Feb '03  
    GeneralRe: Out of memory error with Sybase Pin
    Wes Jones
    9:06 28 Jul '06  
    GeneralHow to Delete or Modify a BLOB? Pin
    Mate
    16:48 7 Jan '03  
    GeneralRe: How to Delete or Modify a BLOB? Pin
    Ben Mathews
    11:42 13 Feb '03  
    GeneralRe: How to Delete or Modify a BLOB? Pin
    ipadilla
    7:31 4 Sep '05  
    GeneralProblem while updating the BLOB to ORACLE using CLongBinary Pin
    Mytreya
    4:14 25 Mar '02  
    GeneralRe: Problem while updating the BLOB to ORACLE using CLongBinary Pin
    DiWa
    6:18 7 Aug '02  


    Last Updated 30 Nov 1999 | Advertise | Privacy | Terms of Use | Copyright © CodeProject, 1999-2009