//////////在选择列表框的时候调用///////////////////
void CADOTest1Dlg::OnItemchangedUserlist(NMHDR* pNMHDR, LRESULT* pResult)
{
NM_LISTVIEW* pNMListView = (NM_LISTVIEW*)pNMHDR;
if(pNMListView->uNewState&LVIS_SELECTED)
{
UpdateData();
SaveData();///保存旧数据
m_nCurrentSel = pNMListView->iItem;
LoadData();///加载新数据
m_cDelItem.EnableWindow();
}
*pResult = 0;
}
//////将记录集中的数据加载到编辑框/////
void CADOTest1Dlg::LoadData()
{
m_pRecordset->Move(m_nCurrentSel,_variant_t((long)adBookmarkFirst));
vUserID = m_pRecordset->GetCollect("ID");
vUsername = m_pRecordset->GetCollect("username");
vOld = m_pRecordset->GetCollect("old");
vBirthday = m_pRecordset->GetCollect("birthday");
m_nUserID = vUserID.lVal;
m_sUsername = (LPCTSTR)(_bstr_t)vUsername;
m_nOld = vOld.lVal;
m_tBirthday = vBirthday;
UpdateData(FALSE);
}
/////将编辑框的数据保存到记录集与列表框
void CADOTest1Dlg::SaveData()
{
if(!m_pRecordset->adoEOF && m_nCurrentSel >= 0 && m_bAutoSave)
{
vUserID = (long)m_nUserID;
vUsername = m_sUsername;
vOld = (long)m_nOld;
vBirthday = m_tBirthday;
m_pRecordset->PutCollect("ID",vUserID);
m_pRecordset->PutCollect("username",vUsername);
m_pRecordset->PutCollect("old",vOld);
m_pRecordset->PutCollect("birthday",vBirthday);
m_userlist.SetItem(m_nCurrentSel,0,LVIF_TEXT,(_bstr_t)vUserID,NULL,0,0,0);
m_userlist.SetItem(m_nCurrentSel,1,LVIF_TEXT,(_bstr_t)vUsername,NULL,0,0,0);
m_userlist.SetItem(m_nCurrentSel,2,LVIF_TEXT,(_bstr_t)vOld,NULL,0,0,0);
m_userlist.SetItem(m_nCurrentSel,3,LVIF_TEXT,(_bstr_t)vBirthday,NULL,0,0,0);
}
}
////////////////////////////////////////////////////////////////////////
BOOL CADOTest1App::InitInstance()
{
AfxEnableControlContainer();
AfxOleInit();///初始化COM库
HRESULT hr; ////////////连接数据库//////////////
try
{
hr = m_pConnection.CreateInstance("ADODB.Connection");///创建Connection对象
if(SUCCEEDED(hr))
{
hr = m_pConnection->Open("Provider=Microsoft.Jet.OLEDB.4.0;
Data Source=test.mdb","","",adModeUnknown);///连接数据库
///上面一句中连接字串中的Provider是针对ACCESS2000环境的,
/////对于ACCESS97,需要改为:Provider=Microsoft.Jet.OLEDB.3.51; }
}
}
catch(_com_error e)///捕捉异常
{
CString errormessage;
errormessage.Format("连接数据库失败!\r\n错误信息:%s",e.ErrorMessage());
AfxMessageBox(errormessage);///显示错误信息
return FALSE;
}
#ifdef _AFXDLL
Enable3dControls(); // Call this when using MFC in a shared DLL
#else
Enable3dControlsStati