Click to See Complete Forum and Search --> : CDaoDatabase Mayhem..


Verbed
May 22nd, 1999, 12:10 AM
Well, I have been teaching myself DAO w/ MFC for the last 6 hours on and off. I have yet to get anything to work. So, I have created a small example of what I want to do, the code unfortunately doesn't function correctly, I would appreciate it if anyone could tell me what I'm doing wrong. Why Prosise couldn't have covered CDaoDatabase is beyond me..


#include <afx.h>
#include <afxdao.h>
#include <iostream.h>

#define CREATE

class CMySet : public CDaoRecordset {
public:
CString m_strNick;
long m_lTime;

CMySet(CDaoDatabase *pdb);
virtual void DoFieldExchange(CDaoFieldExchange *pFX);
};

CMySet::CMySet(CDaoDatabase *pdb) : CDaoRecordset(pdb) {
m_strNick = "";
m_lTime = 0;
}

void CMySet::DoFieldExchange(CDaoFieldExchange *pFX) {
pFX->SetFieldType(CDaoFieldExchange::outputColumn);
DFX_Text(pFX, "[SEEN_NICK]", m_strNick);
DFX_Long(pFX, "[SEEN_TIME]", m_lTime);
}

void main() {

try{

CDaoDatabase daoDb;
CMySet daoSet(&daoDb);

#ifdef CREATE
daoDb.Create(".\\test.mdb", dbLangGeneral);

daoDb.Execute("CREATE TABLE SEEN ([SEEN_NICK] CHAR(20), [SEEN_TIME] NUMBER);");
daoSet.Open(dbOpenDynaset, "SELECT * FROM SEEN;");

daoSet.AddNew();
daoSet.m_strNick = "Verbed";
daoSet.m_lTime = 19820129;
daoSet.Update();

daoSet.AddNew();
daoSet.m_strNick = "Artos";
daoSet.m_lTime = 12312322;
daoSet.Update();

daoSet.AddNew();
daoSet.m_strNick = "EmC2";
daoSet.m_lTime = 76328126;
daoSet.Update();
#endif

#ifndef CREATE
daoDb.Open(".\\test.mdb");
daoSet.Open(dbOpenDynaset, "SELECT * FROM SEEN;");
daoSet.MoveFirst();

while(!daoSet.IsEOF()) {
cout << daoSet.m_strNick << endl << daoSet.m_lTime << endl;
daoSet.MoveNext();
}
#endif

}
catch(CDaoException *e) {
cout << e->m_pErrorInfo->m_strDescription << endl;
}

AfxDaoTerm();
}



Run this with CREATE defined to create the database, run it without to open the database and iterate through the table.