Solved

Hwo to Store and Retrive BMP's and JPG's into Database thr Recordset...

Posted on 1998-05-10
2
659 Views
Last Modified: 2013-11-20
We need to store some large binary data into Database.It may be of bitmap or jPEG or text file.
How to store them in data base.I think we can make use of "CLong Binary" and corresponding
 "RFX_LongBinary " for CRecordset classes.We are not able to store data and retrive data from table.
Can any body suggest the way of Reading and writing bitmap file from database ,using CLong
binary and RFX_LongBinary. Or any Othere way how to ,deal with BLOB .
The database may be any type oracle or MSAccess etc.It should be database independent.



Here is Sample Code... which we wrote...

      CFile strFile;
      BYTE* byt;
      DWORD FileLth;
      long nLen(3);
      CBlobTest   blobObj;

      strFile.Open("C:\\VC++\\Mahesh.bmp",CFile::typeBinary | CFile::modeRead);

      FileLth = strFile.GetLength();
      ByteObj.SetSize(FileLth);

      byt = new BYTE[FileLth];
      DWORD ltemp = strFile.ReadHuge(byt,FileLth);
      ByteObj.Add(*byt);
                        //Here the length is incresing by 1 byte ,i did't got why it is incresed by 1 byte //after
      calling this function.
      delete byt;

      blobObj.SetBlob(&ByteObj, nLen); // This is function of class which is derived
      from CRecordset.

      Code for the SetBlob function.
      try
      {

      if(!IsOpen())
         Open();

      m_Size = pBlob->GetSize();
      AddNew();
      m_BlobValue.Copy(*pBlob);
      m_Size = m_BlobValue.GetSize();
      m_BlobID = nID;
      SetFieldDirty(&m_BlobValue, TRUE);
      Update();
      AfxMessageBox("Blob is Stored into Database");
      return 0;
      }
      catch(CDBException* fe)
      {

      AfxMessageBox(fe->m_strError);
      return 1;
      }

      I Checked for Size of CByteArray before storing ,Before copying into  pBlob it is one less than
      ,after coping from ByteOb to pBlob.

      It is storing into database.Presently we are using MS-Access,We are using field type is Oleobject
      .If We double click onthe field value,it can't be opened by Paintbrush or with some other.Its saying
      oleserver could not found.restart Ms-access and set the oleserver.I did't know whether image is
      properly stored or not.

      While retiving the blob the function is

      int CBlobTest :: GetBlob(CByteArray*  pBlob,long& nID)
      {

      CFile strFile;
      DWORD FileLth;
      BYTE                       byt;
      strFile.Open("C:\\VC++\\Murali.bmp",CFile::typeBinary |
      CFile::modeWrite                        |CFile::modeCreate);

      try
      {
      m_strFilter = "BlobID = 3";

      if(!IsOpen())
         Open();
      else
      Requery();

      FileLth = m_BlobValue.GetSize();

        for(int i = 1 ; i < FileLth ;i++)
      {

      byt  = m_BlobValue.GetAt(i);
      strFile.Write(&byt,1);
      byt = NULL;

                      strFile.Close();
      AfxMessageBox("Field is Loaded from database");

      }


       It is writing some bytes into file.But i could 't open it with Paintbursh or lview or someother.I did't
      Got what is happing there.It seems corretly writing but ,could''nt get sloved.Can U Please help
      Us.Please....

      Can u Please give the complete source code for this.

      BestRegards
      Mahesh
0
Comment
Question by:yakkali
2 Comments
 

Accepted Solution

by:
yangjun earned 120 total points
ID: 1309068
You need to set the following values

m_BlobVlaue.m_hData = memory handle
SetFieldDirty(&m_BlobValue, TRUE);
SetFieldNull( &m_BlobValue, FALSE );
Update();

0
 

Author Comment

by:yakkali
ID: 1309069
Thank U.We can able to store and retrive bmp and jpgs
0

Featured Post

Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
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.
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …

777 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