Collection Serialization

database connection string

2025-11-10

Written by: xiaobin

Usage Collection Serialization

first declare a member variable of type CObArray.

To serialize a CObArray collection in your project, all of you have to do is to call the CObArray::Serialize() member function. Everything else would be handled behind the scenes.

define

private:
    CObArray ListOfConnStrings;

impl

On the main menu, click Project -> Class Wizard…
In the Class Name, make sure CMFCApplication1Dlg is selected.
Click the Virtual Functions tab
In the Virtual Functions list, scroll down and double-click Serialize
Click Edit Code and implement the member function as follows:

void CMFCApplication1Dlg::Serialize(CArchive &ar)
{
    if (ar.IsStoring())
    {   // storing code
    }
    else
    {   // loading code
    }
    ListOfConnStrings.Serialize(ar);
}

project

Editor

ServerDBUserPwdType
.c:\fde.mdbadminwwsrts12MS-Access
serverdbpubssaqwertyMSSQL
192.168.1.107employeesDBAxb12345MySQL

add dialog

IDC_DIALOGBAR

and set:

add class

Add a class from the control

Add Variable

event

Add event handlers to the main dialog box.

New

    int iCurSel = 0;
    iCurSel = m_Types.GetCurSel();

    CConnEditorDlg dlg;

    if (dlg.DoModal() == IDOK)
    {
        CConnectString *connStr = new CConnectString(dlg.m_Server,
            dlg.m_Db, dlg.m_User,
            dlg.m_Pwd, iCurSel);
        // Add that new connect string to the collection
        ListOfConnStrings.Add(connStr);
    }

Save

    CFile fileConnStrs;
    CString strFilename = _T("conns.str");

    fileConnStrs.Open(strFilename, CFile::modeCreate | CFile::modeWrite);
    CArchive arc(&fileConnStrs, CArchive::store);
    Serialize(arc);

    arc.Close();
    fileConnStrs.Close();

Ref