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);���
if (bStore)
{
CArchive ar( &byFile, CArchive::store);
Serialize(ar);
ar.Close();
}
else
{
CArchive ar( &byFile, CArchive::load);
Serialize(ar);���
ar.Close();
}
byFile.Detach();
}
In the file which used
CDaoRecordset
derived a class such as
CSomeDaoRecordset
CSomeDaoRecordset rs;���
CSomeClass object;
rs.Open(. . .);
rs.AddNew();
object.TransferDataWithByteArray(rs.m_byteLongField, TRUE );
. . .
rs.Update();
rs.Close();
rs.Open(. . .);
object.TransferDataWithByteArray(rs.m_byteLongField, FALSE );
...
rs.Close();