Visit the Ultimate Toolbox main page for an overview and configuration guide to the Ultimate Toolbox library.
Contents
The Ultimate Toolbox Database category contains two classes dealing with Data Access Objects and CSV files.
COXCsvFile
extends the MFC class CStdioFile
by adding functions for processing the lines of a CStdioFile
as rows in a comma delimited data file (CSV file).
The
samples\database\CsvTest sample in action.
The following code is taken from the sample, and reads in csv data in a pre-defined format.
void CCsvTestDlg::OnOK()
{
UpdateData();
COXCsvFile inFile;
CFileException fe;
LPCTSTR lpstrInFilename=(m_bUseHeaders ? _T(
"headers.csv") : _T("noheader.csv"));
SData data;
m_aData.RemoveAll();
m_ctrlSave.EnableWindow(FALSE);
if (!inFile.Open(lpstrInFilename, CFile::modeRead, &fe))
{
return ;
}
TRY
{
CWaitCursor cursor;
short nId;
int nYesNo;
int index;
OXTRACE(_T("CCsvTestDlg::OnOK()"));
if (m_bUseHeaders)
{
inFile.GetColumns(8);
inFile.SetAliases(headers[3], aliases);
}
else
{
inFile.SetColumns(headers);
}
while (inFile.ReadLine())
{
data.Clear();
if (inFile.IsLineEmpty())
{
OXTRACE(_T("Reached the end of the first table"));
break;
}
OXTRACE(_T("Reading next line"));
inFile.ReadColumn(_T("ID"), data.nId);
OXTRACE_WRITEVAL(_T("ID"), data.nId);
inFile.ReadColumn(_T("Name"), data.strName);
OXTRACE_WRITEVAL(_T("Name"), data.strName);
inFile.ReadColumn(_T("Byte"), data.ucByte);
OXTRACE_WRITEVAL(_T("Byte"), data.ucByte);
inFile.ReadColumn(_T("Integer"), data.nInt);
OXTRACE_WRITEVAL(_T("Integer"), data.nInt);
inFile.ReadColumn(_T("Float"), data.fFloat);
OXTRACE_WRITEVAL(_T("Float"), data.fFloat);
inFile.ReadColumn(_T("Double"), data.fDouble);
OXTRACE_WRITEVAL(_T("Double"), data.fDouble);
inFile.ReadColumn(_T("String"), data.strString);
OXTRACE_WRITEVAL(_T("String"), data.strString);
inFile.ReadColumn(_T("Valid"), nYesNo, lpstrYesNo);
OXTRACE_WRITEVAL(_T("Valid"), nYesNo);
data.bValid = (nYesNo == 0);
m_aData.Add(data);
}
if (m_bUseHeaders)
{
inFile.GetColumns(3);
}
else
{
inFile.SetColumns(headers2);
while (inFile.ReadLine())
{
if (inFile.IsLineEmpty())
{
continue;
}
OXTRACE(_T("Reading next line"));
inFile.ReadColumn(_T("ID"), nId);
OXTRACE_WRITEVAL(_T("ID"), nId);
for (index = 0 ; index < m_aData.GetSize() ; ++index)
{
if (m_aData[index].nId == nId)
{
break;
}
}
if (index >= m_aData.GetSize())
{
OXTRACE_WRITEVAL(_T(
"ID not found from earlier table"), nId);
continue;
}
inFile.ReadColumn(_T("Last Name"),
m_aData[index].strLastName);
OXTRACE_WRITEVAL(_T("Last Name"),
m_aData[index].strLastName);
inFile.ReadColumn(_T("Address"),
m_aData[index].strAddress);
OXTRACE_WRITEVAL(_T("Address"),
m_aData[index].strAddress);
}
m_strData.Empty();
for (index = 0 ; index < m_aData.GetSize() ; ++index)
{
CString strTemp;
data = m_aData[index];
strTemp.Format(_T("%u. %s %s, %s\r\n")
_T(" %u, %d, %f, %f, %s\r\n")
_T(" \"%s\"\r\n"),
data.nId, data.strName, data.strLastName,
data.strAddress, data.ucByte, data.nInt,
data.fFloat, data.fDouble,
(data.bValid ? _T(
"Valid") : _T("Invalid")),
data.strString);
m_strData += strTemp;
}
UpdateData(FALSE);
m_ctrlSave.EnableWindow(m_aData.GetSize() > 0);
}
CATCH_ALL(e)
{
inFile.Abort();
}
END_CATCH_ALL
}
See the compiled HTML help for more on using COXCsvFile
.
The
samples\database\DAOClass sample in action.
The COXDao
class wraps all of MFC's standard DAO classes (CDaoDatabase
, CDaoTableDef
, CDaoQueryDef
and CDaoRecordset
) into one easy to use class.
All of the necessary DAO classes are created and maintained internally. The most commonly used features are exposed through this class, plus there is easy access to the internal objects if extra functionality is required. For a general DAO overview please refer to the On-Line help supplied with Visual C++.
This class is very straightforward to use and allows for common database tasks to be accomplished in only a couple lines of code. The first step is to create a database object of type COXDao
. Next, open the database using either the COXDao::Open
or COXDao::OpenSQL
commands. Once open, the database is ready for use. Once finished with the database the object can be destroyed, this closes any open database automatically. Also you can reuse the same database object multiple times, since the COXDao::Open
or COXDao::OpenSQL
commands will automatically close any previously opened database.
Usage
COXDao dao;
CString str;
dao.OpenSQL("C:\\MyDatabase.mdb", "SELECT * from MyTable");
dao.GetField("LastName", str);
See the Database | COXDao section of the compiled HTML help file for a complete COXDao
class reference.
Initial CodeProject release August 2007.