Click here to Skip to main content
Click here to Skip to main content
Go to top

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
      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

Share

About the Author

No Biography provided

Comments and Discussions

 
GeneralDAO recordsets not used in demo project PinmemberBaz18-Apr-01 22:39 

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

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

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