database connection string
2025-11-10
Written by: xiaobin
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.
private:
CObArray ListOfConnStrings;
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);
}
| Server | DB | User | Pwd | Type |
|---|---|---|---|---|
| . | c:\fde.mdb | admin | wwsrts12 | MS-Access |
| serverdb | pubs | sa | qwerty | MSSQL |
| 192.168.1.107 | employees | DBA | xb12345 | MySQL |

and set:

Add event handlers to the main dialog box.
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);
}
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();