cbytearrayfile.txt
来自「几个老外写的源代码大家块块下载」· 文本 代码 · 共 57 行
TXT
57 行
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 + =
减小字号Ctrl + -
显示快捷键?