Question : CRecordset and CLongBinary (BLOB problem)

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 !!

Answer : CRecordset and CLongBinary (BLOB problem)

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!!


Random Solutions  
 
programming4us programming4us