📄 cbytearrayfile.txt
字号:
CByteArrayFile -- A class used to serialize object with database field
--------------------------------------------------------------------------------
This article was contributed by You HongJiang.
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();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -