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

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, […]

Written By
CodeGuru Staff
CodeGuru Staff
Apr 3, 1999
1 minute read
CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More

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
  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 class such as CSomeDaoRecordset

/////////////////////////////////////////////////////////////////////////////////////////////
CSomeDaoRecordset rs;    // rs.m_byteLongField is a CByteArray class
CSomeClass object;
// 1. Write to record
rs.Open(. . .);
rs.AddNew();
object.TransDataWithByteArray(rs.m_byteLongField, TRUE /* store */);
. . .
rs.Update();
rs.Close();
// 2. Read from record
rs.Open(. . .);
object.TransDataWithByteArray(rs.m_byteLongField, FALSE /* load */);
…
rs.Close();

Download source – 2 KB

Date Last Updated: April 3, 1999

CodeGuru Logo

CodeGuru covers topics related to Microsoft-related software development, mobile development, database management, and web application programming. In addition to tutorials and how-tos that teach programmers how to code in Microsoft-related languages and frameworks like C# and .Net, we also publish articles on software development tools, the latest in developer news, and advice for project managers. Cloud services such as Microsoft Azure and database options including SQL Server and MSSQL are also frequently covered.

Property of TechnologyAdvice. © 2026 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.