5.初始化CListCtrl控件 CString temp[4]={"ID","stuName","age","score"}; for (int i=0;i<4;i++) { m_listctrl.InsertColumn(i,temp[i],LVCFMT_LEFT,100); } CListCtrl *pmyListCtrl=(CListCtrl *)GetDlgItem(IDC_LIST2); DWORD dwStyle =GetWindowLong(pmyListCtrl->m_hWnd,GWL_STYLE); SetWindowLong(pmyListCtrl->m_hWnd,GWL_STYLE,dwStyle|LVS_REPORT); DWORD styles=pmyListCtrl->GetExtendedStyle(); pmyListCtrl->SetExtendedStyle(styles|LVS_EX_FULLROWSELECT|LVS_EX_GRIDLINES); 6.查找后显示 void CTextDlg::showtable() { CString strSQL; CString strID,strName,strAge,strScore; _variant_t var; _RecordsetPtr ptr;//额外添加 UpdateData(true); int index=0; m_listctrl.DeleteAllItems(); strSQL.Format("select * from student"); //格式化SQL语句 //zhongdian ptr=((CTextApp*)AfxGetApp())->ado.ExecuteSQLSentence(strSQL); // if(!ptr->BOF) { ptr->MoveFirst(); } else return; while(!ptr->adoEOF) //当记录集指针不是指向最后一条的下面时 { var=ptr->GetCollect("ID"); //获取字段值ID的值 strID=""; if (var.vt!=NULL) { strID=(LPCSTR)_bstr_t(var); // 将获取的值赋给strID } var=ptr->GetCollect("stuName"); strName=""; if (var.vt!=NULL) { strName=(LPCSTR)_bstr_t(var); } var=ptr->GetCollect("age"); strAge=""; if (var.vt!=NULL) { strAge=(LPCSTR)_bstr_t(var); } var=ptr->GetCollect("score"); strScore=""; if (var.vt!=NULL) { strScore=(LPCSTR)_bstr_t(var); } m_listctrl.InsertItem(index,strID); m_listctrl.SetItemText(index,1,strName); m_listctrl.SetItemText(index,2,strAge); m_listctrl.SetItemText(index,3,strScore); ptr->MoveNext(); index++; UpdateData(false); } } 7.修改记录集 void CTextDlg::OnButton4() //改 { // TODO: Add your control notification handler code here CString strSQL; CString strID,strName,strAge,strScore; _variant_t var; _RecordsetPtr ptr;//额外添加 UpdateData(true); int index=0; strSQL.Format("update student set stuName='%s' where ID='%s'",m_strName,m_strID); //格式化SQL语句 //zhongdian ptr=((CTextApp*)AfxGetApp())->ado.ExecuteSQLSentence(strSQL); } 8.查找记录集 void CTextDlg::OnButton3() //查找 { // TODO: Add your control notification handler code here // showtable(); CString strSQL; CString strID,strName,strAge,strScore; _variant_t var; _RecordsetPtr ptr;//额外添加 UpdateData(true); int index=0; m_listctrl.DeleteAllItems(); strSQL.Format("select * from student where ID='%s'",m_strID); //格式化SQL语句 //zhongdian ptr=((CTextApp*)AfxGetApp())->ado.ExecuteSQLSentence(strSQL); // if(!ptr->BOF) { ptr->MoveFirst(); } else return; while(!ptr->adoEOF) //当记录集指针不是指向最后一条的下面时 { var=ptr->GetCollect("stuName"); //获取字段值ID的值 strName=""; if (var.vt!=NULL) { strName=(LPCSTR)_bstr_t(var); // 将获取的值赋给strID } m_strName=strName; UpdateData(false); break; } }