Solved

CRecordset and CLongBinary (BLOB problem)

Posted on 1998-08-20
3
1,162 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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
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.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

757 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now