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



Comments

  • How can I read data of memo type from an access database? Help me ! ! !

    Posted by Legacy on 09/10/2003 12:00am

    Originally posted by: shape

    How can I read data of memo type from an access database? Help me ! ! !
    

    Reply
  • How can I add the pictures to the database

    Posted by Legacy on 07/02/2002 12:00am

    Originally posted by: Alexber

    Help pls !
    I need to add the pictures from the file to DB and then read them again...
    void SetFieldValue( LPCTSTR lpszName, const COleVariant& varValue );
    How can I attach the picture to varValue ?

    Reply
  • how can i create a table for storing images, movies and text file using sql?

    Posted by Legacy on 04/18/2002 12:00am

    Originally posted by: jim

    will you kindly give me some help please?

    Reply
  • employee record

    Posted by Legacy on 02/17/2002 12:00am

    Originally posted by: Omolayo M

    I am trying to write an employee record that will retrieve picture from a table an nwind.mdb access file my problem is that after creating the formview I could bind everything with the exception of photo column to the formview is there any way in which you can help

    Reply
  • Need to Add SetFieldDirty(...) and SetFieldNull(...) to make it work

    Posted by Legacy on 08/20/2001 12:00am

    Originally posted by: chg

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

    //Add Here-------

    rs.SetFieldDirty(&rs.m_byteLongField);
    rs.SetFieldNull(&rs.m_byteLongField);

    //---------------


    . . .
    rs.Update();
    rs.Close();

    Reply
  • Will this code help to store the contents of a TXT file

    Posted by Legacy on 07/26/2000 12:00am

    Originally posted by: nizam

    Hi,

    Will you kind of kind help me store the content of a TXT file in the database. And when I retrieve and load the contents into TXT file will it be in the same format.

    Please Confirm

    West Wishes
    Nizam

    Reply
  • Will this code help to store the contents of a TXT file

    Posted by Legacy on 07/26/2000 12:00am

    Originally posted by: nizam

    Hi,

    Will you kind of kind help me store the content of a TXT file in the database. And when I retrieve and load the contents into TXT file will it be in the same format.

    Please Confirm

    West Wishes
    Nizam

    Reply
  • How can I store a bitmap in a mdb file?

    Posted by Legacy on 07/05/1999 12:00am

    Originally posted by: Tiger

    I try to build a database,which has a field named "image",the data type is OLE Object.Then I drived a class from CDaoRecordSet(the DSN is the database I just build),Now I want to store a bitmap in this database,how can I do?

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

Top White Papers and Webcasts

  • With JRebel, developers get to see their code changes immediately, fine-tune their code with incremental changes, debug, explore and deploy their code with ease (both locally and remotely), and ultimately spend more time coding instead of waiting for the dreaded application redeploy to finish. Every time a developer tests a code change it takes minutes to build and deploy the application. JRebel keeps the app server running at all times, so testing is instantaneous and interactive.

  • Live Event Date: May 6, 2014 @ 1:00 p.m. ET / 10:00 a.m. PT While you likely have very good reasons for remaining on WinXP after end of support -- an estimated 20-30% of worldwide devices still are -- the bottom line is your security risk is now significant. In the absence of security patches, attackers will certainly turn their attention to this new opportunity. Join Lumension Vice President Paul Zimski in this one-hour webcast to discuss risk and, more importantly, 5 pragmatic risk mitigation techniques …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds