Click here to Skip to main content
13,352,280 members (71,170 online)
Click here to Skip to main content
Add your own
alternative version


26 bookmarked
Posted 17 Mar 2000

CByteArrayFile - storing objects in databases using DAO

, 17 Mar 2000
Rate this:
Please Sign up or sign in to vote.
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

   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 a class such as CSomeDaoRecordset
CSomeDaoRecordset rs;    // rs.m_byteLongField is a CByteArray class
CSomeClass object;

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

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



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


About the Author

No Biography provided

You may also be interested in...

Comments and Discussions

QuestionEqual ??? Pin
Hadi_Rezaie2-Jul-01 21:12
memberHadi_Rezaie2-Jul-01 21:12 
GeneralDAO recordsets not used in demo project Pin
Baz18-Apr-01 23:39
memberBaz18-Apr-01 23:39 
QuestionHow do you put in the CHM file? Pin
Tao Lin25-Mar-01 17:22
memberTao Lin25-Mar-01 17: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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.180111.1 | Last Updated 18 Mar 2000
Article Copyright 2000 by Daniel Kaminski
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid