void CMyDAODemoView::OnInitialUpdate()
{
CListView::OnInitialUpdate();
// TODO: You may populate your ListView with items by directly accessing
// its list control through a call to GetListCtrl().
CListCtrl& ctrlList = (CListCtrl&) GetListCtrl();
ctrlList.SetExtendedStyle(LVS_EX_FULLROWSELECT);
m_pDatabase = new CDaoDatabase;
try
{
m_pDatabase->Open("..\\Assist.mdb");
m_pRecordset = new CDaoRecordset(m_pDatabase);
}
catch (CDaoException* e)
{
e->ReportError();
delete m_pDatabase;
m_pDatabase = NULL;
e->Delete();
return;
}
}
void CMyDAODemoView::OnDestroy()
{
CListView::OnDestroy();
// TODO: Add your message handler code here
if(m_pRecordset){
if (m_pRecordset->IsOpen()) m_pRecordset->Close();
delete m_pRecordset;
}
if(m_pDatabase){
if (m_pDatabase->IsOpen()) m_pDatabase->Close();
delete m_pDatabase;
}
}
void CMyDAODemoView::OnStuffView()
{
// TODO: Add your command handler code here
m_strTableName = _T("物品");
if(!m_pDatabase->IsOpen()) return;
if(!m_pRecordset) return;
if(m_pRecordset->IsOpen()) m_pRecordset->Close();
CListCtrl& ctlList = (CListCtrl&)GetListCtrl();
EraseList();
// get table information
CDaoFieldInfo fieldInfo;
int nFields;
CDaoTableDef td(m_pDatabase);
try
{
td.Open(m_strTableName);
nFields = td.GetFieldCount();
for (int j=0; j < nFields; j++)
{
td.GetFieldInfo(j,fieldInfo);
int nWidth = ctlList.GetStringWidth(fieldInfo.m_strName) + 15;
ctlList.InsertColumn(j,fieldInfo.m_strName, LVCFMT_LEFT,
nWidth);
}
}
catch (CDaoException* e)
{
e->ReportError();
e->Delete();
return;
}
td.Close();
int nItem = 0;
try
{
CString strSelect(_T("Select * From ["));
strSelect += m_strTableName;
strSelect += _T("]");
m_pRecordset->Open(dbOpenDynaset,strSelect);
while (!m_pRecordset->IsEOF()) {
COleVariant var;
var = m_pRecordset->GetFieldValue(0);
ctlList.InsertItem(nItem,CCrack::strVARIANT(var));
for (int i=0; i < nFields; i++)
{
var = m_pRecordset->GetFieldValue(i);
ctlList.SetItemText( nItem,i,CCrack::strVARIANT(var));
}
nItem++;
m_pRecordset->MoveNext();
}
}
catch (CDaoException* e)
{
e->ReportError();
e->Delete();
return;
}
CString strRecCount;
strRecCount.Format(_T("显示了%d条记录。"),nItem);
UpdateWindow();
if (nItem>=MAXRECORDS) MessageBox(strRecCount);
((CFrameWnd *) AfxGetMainWnd())->SetMessageText(strRecCount);
}
class CCrack
{
public:
static LPCTSTR strFieldType(short sType);
static LPCTSTR strQueryDefType(short sType);
static LPCTSTR strBOOL(BOOL bFlag);
static CString strVARIANT(const COleVariant& var);
};
LPCTSTR CCrack::strFieldType(short sType)
{
switch(sType){
case (dbBoolean):
return _T("Bool");
case (dbByte):
return _T("Byte");
case (dbInteger):
return _T("Integer");
case (dbLong):
return _T("Long");
case (dbCurrency):
return _T("Currency");
case (dbSingle):
return _T("Single");
case (dbDouble):
return _T("Double");
case (dbDate):
return _T("Date");
case (dbText):
return _T("Text");
case (dbLongBinary):
return _T("Long Binary");
case (dbMemo):
return _T("Memo");
case (dbGUID):
return _T("GUID");
}
return _T("Unknown");
}
LPCTSTR CCrack::strQueryDefType(short sType)
{
switch(sType){
case (dbQSelect):
return _T("Select");
case (dbQAction):
return _T("Action");
case (dbQCrosstab):
return _T("Crosstab");
case (dbQDelete):
return _T("Delete");
case (dbQUpdate):
return _T("Update");
case (dbQAppend):
return _T("Append");
case (dbQMakeTable):
return _T("MakeTable");
case (dbQDDL):
return _T("DDL");
case (dbQSQLPassThrough):
return _T("SQLPassThrough");
case (dbQSetOperation):
return _T("Set Operation");
case (dbQSPTBulk):
return _T("SPTBulk");
}
return _T("Unknown");
}
LPCTSTR CCrack::strBOOL(BOOL bFlag)
{
return bFlag ? _T("TRUE") : _T("FALSE");
}
CString CCrack::strVARIANT(const COleVariant& var)
{
CString strRet;
strRet = _T("Fish");
switch(var.vt){
case VT_EMPTY:
case VT_NULL:
strRet = _T("NULL");
break;
case VT_I2:
strRet.Format(_T("%hd"),V_I2(&var));
break;
case VT_I4:
strRet.Format(_T("%d"),V_I4(&var));
break;
case VT_R4:
strRet.Format(_T("%e"),(double)V_R4(&var));
break;
case VT_R8:
strRet.Format(_T("%e"),V_R8(&var));
break;
case VT_CY:
strRet = COleCurrency(var).Format();
break;
case VT_DATE:
strRet = COleDateTime(var).Format(_T("%m %d %y"));
break;
case VT_BSTR:
strRet = V_BSTRT(&var);
break;
case VT_DISPATCH:
strRet = _T("VT_DISPATCH");
break;
case VT_ERROR:
strRet = _T("VT_ERROR");
break;
case VT_BOOL:
return strBOOL(V_BOOL(&var));
case VT_VARIANT:
strRet = _T("VT_VARIANT");
break;
case VT_UNKNOWN:
strRet = _T("VT_UNKNOWN");
break;
case VT_I1:
strRet = _T("VT_I1");
break;
case VT_UI1:
strRet.Format(_T("0x%02hX"),(unsigned short)V_UI1(&var));
break;
case VT_UI2:
strRet = _T("VT_UI2");
break;
case VT_UI4:
strRet = _T("VT_UI4");
break;
case VT_I8:
strRet = _T("VT_I8");
break;
case VT_UI8:
strRet = _T("VT_UI8");
break;
case VT_INT:
strRet = _T("VT_INT");
break;
case VT_UINT:
strRet = _T("VT_UINT");
break;
case VT_VOID:
strRet = _T("VT_VOID");
break;
case VT_HRESULT:
strRet = _T("VT_HRESULT");
break;
case VT_PTR:
strRet = _T("VT_PTR");
break;
case VT_SAFEARRAY:
strRet = _T("VT_SAFEARRAY");
break;
case VT_CARRAY:
strRet = _T("VT_CARRAY");
break;
case VT_USERDEFINED:
strRet = _T("VT_USERDEFINED");
break;
case VT_LPSTR:
strRet = _T("VT_LPSTR");
break;
case VT_LPWSTR:
strRet = _T("VT_LPWSTR");
break;
case VT_FILETIME:
strRet = _T("VT_FILETIME");
break;
case VT_BLOB:
strRet = _T("VT_BLOB");
break;
case VT_STREAM:
strRet = _T("VT_STREAM");
break;
case VT_STORAGE:
strRet = _T("VT_STORAGE");
break;
case VT_STREAMED_OBJECT:
strRet = _T("VT_STREAMED_OBJECT");
break;
case VT_STORED_OBJECT:
strRet = _T("VT_STORED_OBJECT");
break;
case VT_BLOB_OBJECT:
strRet = _T("VT_BLOB_OBJECT");
break;
case VT_CF:
strRet = _T("VT_CF");
break;
case VT_CLSID:
strRet = _T("VT_CLSID");
break;
}
WORD vt = var.vt;
if(vt & VT_ARRAY){
vt = vt & ~VT_ARRAY;
strRet = _T("Array of ");
}
if(vt & VT_BYREF){
vt = vt & ~VT_BYREF;
strRet += _T("Pointer to ");
}
if(vt != var.vt){
switch(vt){
case VT_EMPTY:
strRet += _T("VT_EMPTY");
break;
case VT_NULL:
strRet += _T("VT_NULL");
break;
case VT_I2:
strRet += _T("VT_I2");
break;
case VT_I4:
strRet += _T("VT_I4");
break;
case VT_R4:
strRet += _T("VT_R4");
break;
case VT_R8:
strRet += _T("VT_R8");
break;
case VT_CY:
strRet += _T("VT_CY");
break;
case VT_DATE:
strRet += _T("VT_DATE");
break;
case VT_BSTR:
strRet += _T("VT_BSTR");
break;
case VT_DISPATCH:
strRet += _T("VT_DISPATCH");
break;
case VT_ERROR:
strRet += _T("VT_ERROR");
break;
case VT_BOOL:
strRet += _T("VT_BOOL");
break;
case VT_VARIANT:
strRet += _T("VT_VARIANT");
break;
case VT_UNKNOWN:
strRet += _T("VT_UNKNOWN");
break;
case VT_I1:
strRet += _T("VT_I1");
break;
case VT_UI1:
strRet += _T("VT_UI1");
break;
case VT_UI2:
strRet += _T("VT_UI2");
break;
case VT_UI4:
strRet += _T("VT_UI4");
break;
case VT_I8:
strRet += _T("VT_I8");
break;
case VT_UI8:
strRet += _T("VT_UI8");
break;
case VT_INT:
strRet += _T("VT_INT");
break;
case VT_UINT:
strRet += _T("VT_UINT");
break;
case VT_VOID:
strRet += _T("VT_VOID");
break;
case VT_HRESULT:
strRet += _T("VT_HRESULT");
break;
case VT_PTR:
strRet += _T("VT_PTR");
break;
case VT_SAFEARRAY:
strRet += _T("VT_SAFEARRAY");
break;
case VT_CARRAY:
strRet += _T("VT_CARRAY");
break;
case VT_USERDEFINED:
strRet += _T("VT_USERDEFINED");
break;
case VT_LPSTR:
strRet += _T("VT_LPSTR");
break;
case VT_LPWSTR:
strRet += _T("VT_LPWSTR");
break;
case VT_FILETIME:
strRet += _T("VT_FILETIME");
break;
case VT_BLOB:
strRet += _T("VT_BLOB");
break;
case VT_STREAM:
strRet += _T("VT_STREAM");
break;
case VT_STORAGE:
strRet += _T("VT_STORAGE");
break;
case VT_STREAMED_OBJECT:
strRet += _T("VT_STREAMED_OBJECT");
break;
case VT_STORED_OBJECT:
strRet += _T("VT_STORED_OBJECT");
break;
case VT_BLOB_OBJECT:
strRet += _T("VT_BLOB_OBJECT");
break;
case VT_CF:
strRet += _T("VT_CF");
break;
case VT_CLSID:
strRet += _T("VT_CLSID");
break;
}
}
return strRet;
}