Solved

CRecordset and CLongBinary (BLOB problem)

Posted on 1998-08-20
3
1,194 Views
Last Modified: 2013-11-20
How do I store a blob through ODBC !
I got a CLongBinary object in a derived CRecordset and a BMP-file on disk.
1. - How do I load the BMP into the CLongBinary-object
2. - Does the CLongBinary automaticly get into the base on CRecordset.update() ?
Codesample please !!

0
Comment
Question by:Jeb
  • 2
3 Comments
 

Accepted Solution

by:
MURALI_YAKKALI earned 80 total points
ID: 1321090
First you have to copy BMP file into CLongBinary object.
you can do this by
Here
 strFileName = BMP file name
 BinaryObj   =  long binary object

:: int  LoadBlobFromFile(CString& strFileName,CLongBinary* BinaryObj)
{

      CFile            blobFile(NULL);
      DWORD            FileLth;
      void*            lVoidPtr = NULL;

      if(blobFile.Open(strFileName,CFile::typeBinary | CFile::modeRead))
      {

            FileLth = blobFile.GetLength();
      
            BinaryObj->m_hData = GlobalAlloc(0,FileLth);
            BinaryObj->m_dwDataLength = FileLth;

            lVoidPtr = GlobalLock(BinaryObj->m_hData);
            DWORD lTemp = blobFile.ReadHuge(lVoidPtr,FileLth);
            GlobalUnlock(BinaryObj->m_hData);
            strFileName = blobFile.GetFileName();
            blobFile.Close();
            return SUCCESS;

      }
      else
            return FAILURE;

} // End of LoadBlobFromFile

After loading bmp into longbinary you have to store this into database
You can do this by
DWORD i,dwSize;
BYTE* lpdstbyte = NULL;
BYTE* lpsrcbyte = NULL;
int :: AddnewRecord(CLongBinary* pDataObj)
{
      try
      {
            if(!IsOpen())
              Open();
            AddNew();  

      if(pDataObj.m_hData && ::GlobalSize(m_DataObj.m_hData))
      {
            ::GlobalFree(m_DataObj.m_hData);
            m_DataObj.m_hData = NULL;
            m_DataObj.m_dwDataLength = 0;
      }

      dwSize = pDataObj.m_dwDataLength;
      m_DataObj.m_hData  = GlobalAlloc(0,dwSize);

      lpdstbyte = (BYTE*)GlobalLock(m_DataObj.m_hData);
      if(lpdstbyte == NULL)
            return *this;

      lpsrcbyte = (BYTE*)GlobalLock(pDataObj.m_hData);
      if(lpsrcbyte == NULL)
      {
            GlobalUnlock(m_DataObj.m_hData);
                return;
      }

      for(i=0; i< dwSize; i++)
      {
            lpdstbyte[i] = lpsrcbyte[i];
      }

      m_DataObj.m_dwDataLength   =  dwSize;

      GlobalUnlock(m_DataObj.m_hData);
      GlobalUnlock(pDataObj.m_hData);
                                       
        SetFieldDirty(&m_DataObj,TRUE);                                    
        SetFieldNull(&m_DataObj,FALSE);      
      Update();
      Close();
        return SUCCESS;

      }
      catch(CDBException* fe)
      {
            return FAILURE;
      }

}

This is complete code for storing blob intodatabase.
Hope this will help U!!


0
 

Author Comment

by:Jeb
ID: 1321091
You saved my holiday :)
0
 

Author Comment

by:Jeb
ID: 1321092
It should have been 80 points instead of 8 !!  sorry
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.

679 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question