Click here to Skip to main content
15,881,852 members
Articles / Database Development / SQL Server
Article

Using CLongBinary for BLOBs

Rate me:
Please Sign up or sign in to vote.
4.96/5 (15 votes)
30 Nov 19991 min read 212.8K   2.5K   52   46
  • 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


    Written By
    United States United States
    This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

    Comments and Discussions

     
    AnswerRe: How to Delete or Modify a BLOB? Pin
    Ben Mathews13-Feb-03 10:42
    Ben Mathews13-Feb-03 10:42 
    AnswerRe: How to Delete or Modify a BLOB? Pin
    ipadilla4-Sep-05 6:31
    ipadilla4-Sep-05 6:31 
    GeneralProblem while updating the BLOB to ORACLE using CLongBinary Pin
    Mytreya25-Mar-02 3:14
    Mytreya25-Mar-02 3:14 
    GeneralRe: Problem while updating the BLOB to ORACLE using CLongBinary Pin
    DiWa7-Aug-02 5:18
    DiWa7-Aug-02 5:18 
    GeneralThanks for your excellent job,by the way,my specific solution Pin
    9-Jan-02 20:54
    suss9-Jan-02 20:54 
    GeneralI have Exception - "Data truncated." Pin
    23-Oct-01 1:15
    suss23-Oct-01 1:15 
    GeneralLong Binary field !!! Pin
    Hadi Rezaee6-Oct-01 17:24
    Hadi Rezaee6-Oct-01 17:24 
    GeneralRe: Long Binary field !!! Pin
    15-Jun-02 7:05
    suss15-Jun-02 7:05 
    I have the same problem. Did you ever figure this out? Can you just use Memo fields?
    QuestionGlobalUnlock?? Pin
    22-Dec-00 15:28
    suss22-Dec-00 15:28 
    GeneralThanks! Pin
    Scott!10-Jul-00 6:58
    Scott!10-Jul-00 6:58 
    QuestionWhy create temporary files? Pin
    Steve Knoblock11-Jun-00 13:58
    Steve Knoblock11-Jun-00 13:58 
    AnswerRe: Why create temporary files? Pin
    Scott!10-Jul-00 6:55
    Scott!10-Jul-00 6:55 
    GeneralUsing CLongBinary with LONG (Oracle) Pin
    Ilango28-May-00 18:58
    Ilango28-May-00 18:58 
    GeneralRe: Using CLongBinary with LONG (Oracle) Pin
    He Huiguang1-Jun-00 2:18
    sussHe Huiguang1-Jun-00 2:18 
    GeneralRe: Using CLongBinary with LONG (Oracle) Pin
    Hassan Zia12-Sep-00 0:18
    Hassan Zia12-Sep-00 0:18 
    GeneralRe: Using CLongBinary with LONG (Oracle) Pin
    3-Feb-01 17:11
    suss3-Feb-01 17:11 
    GeneralRe: Using CLongBinary with LONG (Oracle) Pin
    15-May-01 16:18
    suss15-May-01 16:18 
    GeneralRe: Using CLongBinary with LONG (Oracle) Pin
    21-Jan-02 6:34
    suss21-Jan-02 6:34 
    GeneralRe: Using CLongBinary with LONG (Oracle) Pin
    26-Mar-01 20:41
    suss26-Mar-01 20:41 
    GeneralRe: Using CLongBinary with LONG (Oracle) Pin
    2-Aug-01 18:05
    suss2-Aug-01 18:05 

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

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