Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

CRecordset and CLongBinary (BLOB problem)

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
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.

839 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