void CMyADODlg::OnButtonDel()
{
UpdateData(TRUE);
POSITION pos;
int iPos;
CString strPersonID;
pos = m_listLinkInfo.GetFirstSelectedItemPosition();
if (pos == NULL)
{
::MessageBox(NULL, "请选择要删除的记录!", "通信录", MB_OK|MB_ICONINFORMATION|MB_TASKMODAL);
return;
}
if(AfxMessageBox("真的要删除该记录吗?", MB_ICONQUESTION | MB_YESNO|MB_DEFBUTTON2) == IDNO)
return;
iPos = m_listLinkInfo.GetNextSelectedItem(pos);
strPersonID = m_listLinkInfo.GetItemText(iPos, 5); //获取要删除用户的ID
_ConnectionPtr m_pConnection; //建立连接对象指针
m_pConnection.CreateInstance(__uuidof(Connection));//建立连接对象实例
try
{
// 打开本地Access库AddressBook.mdb
m_pConnection->Open("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=..\\AddressBook.mdb","","",adModeUnknown);
}
catch(_com_error e)
{
AfxMessageBox("数据库连接失败,确认数据库AddressBook.mdb是否在当前路径下!");
return;
}
_variant_t m_pRecordset;
CString strSQL;
strSQL.Format("delete from LinkInfo where PersonID=%s", strPersonID); //构造sql语句
try
{
//使用由本地游标库提供的客户端游标
m_pConnection->CursorLocation = adUseClient;
m_pConnection->Execute(_bstr_t(strSQL), &m_pRecordset, adExecuteNoRecords);
}
catch(_com_error *e)
{
AfxMessageBox(e->ErrorMessage());
return;
}
if(m_pConnection->State)
m_pConnection->Close();
m_pConnection= NULL;
ShowItem("select * from LinkInfo");
UpdateData(FALSE);
}