Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Reading and Writing BLOB into Database thr ODBC

Posted on 1998-04-30
7
Medium Priority
?
846 Views
Last Modified: 2013-11-19
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.

Can any body help us...
0
Comment
Question by:yakkali
  • 4
  • 3
7 Comments
 
LVL 4

Expert Comment

by:piano_boxer
ID: 1304776
Instead of using the CLongBinary class use CByteArray. In this I will name this as m_rgBin.

To save a block of binary data:

*** Before this open recordset and make a call to AddNew()
*** or Edit().

// First set size of CByteArray to the size needed.
rs.m_rgBin.SetSize( nSizeNeeded );

// If you are updating an existing record,
// check if it has changed.
// Otherwize ommit this test.
if(memcmp(&rs.m_rgBin[0], pMyData, rs.m_rgBin.GetSize()))
{
    // Its changed.
    // Copy the memory block into the CByteArrar object
    memcpy(&rs.m_rgBin[0], pMyData, rs.m_rgBin.GetSize());

    // Flag field as having a value, and dirty
    rs.SetFieldNull(&rs.m_rgBin, FALSE);
    rs.SetFieldDirty(&rs.m_rgBin, TRUE);
}

// NOTE: If you want to clear the field in the database
//       make this call: rs.m_rgBin.SetSize(0) and also
//       do the SetFieldDirty() call.

*** Somewhere after this call update to store the data.

---
To load a block of binary data:

// Are the field empty?
if(rs.m_userData.GetSize())
{
    // No, copy to our local buffer pMyData
    memcpy(pMyData, &rs.m_rgBin[0], rs.m_rgBin.GetSize());
}
else
{
    //
    // TODO: Insert code for handling empty field from database.
    //
}

------
THATS IT !!!!!!!!

The key to all this is the two functions SetFieldNull() and SetFieldDirty();

0
 

Author Comment

by:yakkali
ID: 1304777
Setting the size of the CByteArray is only needed when storing.

When you read the field from the database you dont need to set the size, it will be set by The field exchange function RFX_Binary.

After retriving a record you can call GetSize() to determine the size of the CByteArray, thus the size of the binary data read from the database.
0
 
LVL 4

Expert Comment

by:piano_boxer
ID: 1304778
REJECTED ??????????????????????????????????????????????????????????????????????????????????????????
0
Independent Software Vendors: 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!

 
LVL 4

Expert Comment

by:piano_boxer
ID: 1304779

I will explain our problem in detail.We will be getting bmp or jpg from file.We has to read from that file.Then that has to be filled into CByteArray or CLongBinary .We are using CByteArray as u suggested.Then this has to be stored into database.Hwo should get the size.We are able to retive from database ,but gettting some junk data.And can't able to store into database.Can u explain form step1(reading image bmp or jpg)......to store and retive.

0
 

Author Comment

by:yakkali
ID: 1304780
Code for loading BMP/JPEG files (CImage 1.2 class) can be found here:
http://web.ukonline.co.uk/Members/julian.smart/code.html

0
 
LVL 4

Accepted Solution

by:
piano_boxer earned 300 total points
ID: 1304781
Still has some problem.I did't got how that example will help us.Presently we are able to store into database but retrival is the problem.I am here with sample code.

      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
 

Author Comment

by:yakkali
ID: 1304782
F
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

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…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
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.
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
Suggested Courses

783 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