前几天在俺的本子上安装了
1,MySQL server 5.0
2,MySQL tools for 5.0
3,MySQL Connector\ODBC 3.51
然后建立了名为xyx的数据库,在该数据库内建了一个表格dblist,包含ID,NAME,AGE三项内容:很简单,暂且为了熟悉一下vc下访问数据库的一般过程。
(1)建立ODBC数据源,测试连接成功!!
(2)建立一个MFC的对话框应用程序。
(3)在对话框内添加一个list control和四个button(查询,添加,删除,编辑)。
(4)为list control添加控制变量m_list,为查询Button添加点击响应函数OnButtonQuery() 添加如下代码:
// TODO: Add your control notification handler code here
m_list.DeleteAllItems();
CDatabase db;
db.Open(NULL,FALSE,FALSE,"ODBC;DSN=MySQL;UID=root;PWD=xyx");
CRecordset rs( &db );
rs.Open( CRecordset::forwardOnly, "SELECT * FROM dblist"); while(!rs.IsEOF())
{
CString varID;
rs.GetFieldValue("ID", varID); 
m_list.InsertItem(0,varID);
CString varNAME;
rs.GetFieldValue("NAME", varNAME); 
m_list.SetItemText(0, 1, varNAME);
CString varAGE;
rs.GetFieldValue("age", varAGE); 
m_list.SetItemText(0, 2, varAGE);
rs.MoveNext();
}
rs.Close();
db.Close();
   }
单步调试时在红色一行跳到MFC\src\wincore.cpp中
LRESULT lResult;
TRY
{
#ifndef _AFX_NO_OCC_SUPPORT
// special case for WM_DESTROY
if ((nMsg == WM_DESTROY) && (pWnd->m_pCtrlCont != NULL))
pWnd->m_pCtrlCont->OnUIActivate(NULL);
#endif // special case for WM_INITDIALOG
CRect rectOld;
DWORD dwStyle = 0;
if (nMsg == WM_INITDIALOG)
_AfxPreInitDialog(pWnd, &rectOld, &dwStyle); // delegate to object's WindowProc
lResult = pWnd->WindowProc(nMsg, wParam, lParam); // more special case for WM_INITDIALOG
if (nMsg == WM_INITDIALOG)
_AfxPostInitDialog(pWnd, rectOld, dwStyle);
}
CATCH_ALL(e)
{
CWinThread* pWinThread = AfxGetThread();
if ( pWinThread != NULL )
{
弹出错误提示框: lResult = pWinThread->ProcessWndProcException(e, &pThreadState->m_lastSentMsg);
TRACE1("Warning: Uncaught exception in WindowProc (returning %ld).\n",
lResult);
}
else
{
TRACE0("Warning: Uncaught exception in WindowProc.\n");
lResult = 0;
}
DELETE_EXCEPTION(e);
}
END_CATCH_ALL pThreadState->m_lastSentMsg = oldState;
return lResult;
}
弹出的错误提示:无效的字段名和字段索引!请大虾解救!!是不是vc中应该添加某些设置??