Click here to Skip to main content
15,881,757 members
Articles / Desktop Programming / MFC
Article

CByteArrayFile - storing objects in databases using DAO

Rate me:
Please Sign up or sign in to vote.
4.42/5 (6 votes)
17 Mar 2000 75.7K   2.2K   27   3
A class used to serialize object into a database field

You HongJiang contributed the CByteArrayFile class and article, and Daniel Kaminski contributed the demonstration application.

In my project I need to serialize some objects into a database field with DAO. However, I can not find any support for serializing anobject into database field. I had to implemented one by myself.

In this example, I will show you a class named CByteArrayFile. It is a derived class of CMemFile. With this class we can serialize objects into a CByteArray object. We can then use DFX_Binary to transfer data into a database "long binary" field.

Following is the sample code of how to use the CByteArrayFile class.

In the class which need to be serialized (such as CSomeClass):

CSomeClass::TransferDataWithByteArray(CByteArray& byteArray, BOOL bStore)
{
   CByteArrayFile byFile;
   byFile.SetArray(&byteArray);    // attach byteArray with byFile
   
   if (bStore) // store object into byteArray
   {
      // create CArchive object with CByteArray File
      CArchive ar( &byFile, CArchive::store);
 
      // Call CSomeClass::Serialize function
      Serialize(ar);

      ar.Close();
   }
   else // load object from byteArray
   {
     CArchive ar( &byFile, CArchive::load);
     Serialize(ar);    // Call CSomeClass::Serialize function
     ar.Close();
   }
 
  // important!! detach byteArray with byFile. Or byteArray will be 
  // free with byFile
  byFile.Detach();
}
In the file which used CDaoRecordset derived a class such as CSomeDaoRecordset
CSomeDaoRecordset rs;    // rs.m_byteLongField is a CByteArray class
CSomeClass object;

// 1. Write to record
rs.Open(. . .);
rs.AddNew();
object.TransferDataWithByteArray(rs.m_byteLongField, TRUE /* store */);
. . .
rs.Update();
rs.Close();

// 2. Read from record
rs.Open(. . .);
object.TransferDataWithByteArray(rs.m_byteLongField, FALSE /* load */);
...

rs.Close();

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
China China
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionEqual ??? Pin
Hadi Rezaee2-Jul-01 20:12
Hadi Rezaee2-Jul-01 20:12 
GeneralDAO recordsets not used in demo project Pin
Baz18-Apr-01 22:39
Baz18-Apr-01 22:39 
QuestionHow do you put in the CHM file? Pin
Tao Lin25-Mar-01 16:22
Tao Lin25-Mar-01 16:22 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.