CByteArrayFile -- A class used to serialize object with database field


In my project, I need to serialize some object into database field with DAO. However, I can not find any support on serializing object into database field. I have to implemented one by myself.

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

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

In the class which need to be serialize, such as CSomeClass.cpp

CSomeClass::TransDataWithByteArray(CByteArray& byteArray, BOOL bStore)
 CByteArrayFile byFile;
 byFile.SetArray(&byteArray);    // attach byteArray with byFile
 if (bStore) // store object into byteArray
  CArchive ar( &byFile, CArchive::store); // create CArchive object with CByteArray File
  Serialize(ar);    // Call CSomeClass::Serialize function
 else // load object from byteArray
  CArchive ar( &byFile, CArchive::load);
  Serialize(ar);    // Call CSomeClass::Serialize function
 // important!!, detach byteArray with byFile. Or byteArray will be free with byFile

In the file which used CDaoRecordset derived class such as CSomeDaoRecordset

CSomeDaoRecordset rs;    // rs.m_byteLongField is a CByteArray class
CSomeClass object;
// 1. Write to record
rs.Open(. . .);
object.TransDataWithByteArray(rs.m_byteLongField, TRUE /* store */);
. . .

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


Download source - 2 KB

Date Last Updated: April 3, 1999


  • There are no comments yet. Be the first to comment!

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • As all sorts of data becomes available for storage, analysis and retrieval - so called 'Big Data' - there are potentially huge benefits, but equally huge challenges...
  • The agile organization needs knowledge to act on, quickly and effectively. Though many organizations are clamouring for "Big Data", not nearly as many know what to do with it...
  • Cloud-based integration solutions can be confusing. Adding to the confusion are the multiple ways IT departments can deliver such integration...

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date