#define LVCOLUMNS 3
static LVCOLUMN lpSampleDBListColumns[] =
{ { LVCF_TEXT | LVCF_WIDTH, 0, 30, _T("CustID"), 256, 0 },
{ LVCF_TEXT | LVCF_WIDTH, 0, 30, _T("CustName"), 256, 0 },
{ LVCF_TEXT | LVCF_WIDTH, 0, 30, _T("CustQty"), 256, 0 } };
BOOL CSampleDBAppDlg::OnInitDialog()
{
CDialogEx::OnInitDialog();
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);
CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
BOOL bNameValid;
CString strAboutMenu;
bNameValid = strAboutMenu.LoadString(IDS_ABOUTBOX);
ASSERT(bNameValid);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}
SetIcon(m_hIcon, TRUE); SetIcon(m_hIcon, FALSE);
CListCtrl* pListCtrl = NULL;
if ((pListCtrl = (CListCtrl*)GetDlgItem(IDC_SAMPLEDBLIST)) != NULL)
{
CRect lvRect; pListCtrl->GetClientRect(&lvRect);
int nColumnsCount = sizeof(lpSampleDBListColumns) / sizeof(LVCOLUMN);
for (int iIndex = 0; iIndex != nColumnsCount; iIndex++)
{
lpSampleDBListColumns[iIndex].cx = lvRect.Width() / nColumnsCount;
pListCtrl->InsertColumn(iIndex, &lpSampleDBListColumns[iIndex]);
}
}
CDatabase* pdb = new CDatabase();
if (!pdb->OpenEx(_T("DBQ=E:\\SampleDB.mdb;DefaultDir=E:\\;Driver={Driver do Microsoft Access (*.mdb)}; \
DriverId=25;FIL=MS Access;FILEDSN=E:\\TestDB0.dsn;MaxBufferSize=2048;MaxScanRows=8;PageTimeout=5;SafeTransactions=0;Threads=3;UID=admin;UserCommitSync=Yes;")))
{
TRACE0("Unable to connect to database using the data source");
}
CRecordset* pRecordSet = new CRecordset(pdb);
if (!pRecordSet->Open(AFX_DB_USE_DEFAULT_TYPE, _T("SELECT * FROM CUSTOMERS")))
{
TRACE0("Unable to open recordset");
}
pRecordSet->MoveFirst();
for (int iRow = 0; !pRecordSet->IsEOF(); iRow++)
{
CDBVariant varField[3];
pRecordSet->GetFieldValue((short)0, varField[0]);
_TCHAR szField0[256] = _T("\0");
wsprintf(szField0, _T("%d"), varField[0].m_iVal);
pListCtrl->InsertItem(LVIF_TEXT | LVIF_STATE, iRow, szField0, 0, 0, 0, 0);
pRecordSet->GetFieldValue((short)1, varField[1]);
pListCtrl->SetItemText(iRow, 1, static_cast<const>(*varField[1].m_pstringW));
pRecordSet->GetFieldValue((short)2, varField[2]);
_TCHAR szField2[256] = _T("\0");
wsprintf(szField2, _T("%d"), varField[2].m_iVal);
pListCtrl->SetItemText(iRow, 2, szField2);
pRecordSet->MoveNext();
}
pRecordSet->Close();
pdb->Close();
return TRUE; }</const>