ADO数据库操作

时间:2021-03-27 03:16:22

ADO数据库操作

void CSjtestDlg::OnBnClickedButtonAdd()
{
// TODO: 在此添加控件通知处理程序代码
this->ShowWindow(SW_HIDE);
DigAdd dig ;
dig.DoModal() ;
this->ShowWindow(SW_SHOW);
m_Grid.DeleteAllItems() ;
ADOConn m_Adoconn ;
m_Adoconn.OnInitADOConn() ;
CString sql ;
sql.Format(_T("select * from employees order by 员工编号 desc")) ;
m_Adoconn.m_pRecordset = m_Adoconn.GetRecordSet((_bstr_t)sql) ;
while(! m_Adoconn.m_pRecordset->adoEOF)
{
m_Grid.InsertItem(0,_T(""));
m_Grid.SetItemText(0,0,(_bstr_t)m_Adoconn.m_pRecordset->GetCollect(_T("员工编号")));
m_Grid.SetItemText(0,1,(_bstr_t)m_Adoconn.m_pRecordset->GetCollect(_T("员工姓名")));
m_Grid.SetItemText(0,2,(_bstr_t)m_Adoconn.m_pRecordset->GetCollect(_T("所属部门")));
m_Grid.SetItemText(0,3,(_bstr_t)m_Adoconn.m_pRecordset->GetCollect(_T("基本工资")));
//将记录集指针移动到下一条记录
m_Adoconn.m_pRecordset->MoveNext();
}
m_Adoconn.ExitConnect() ;
}

  

ADO数据库操作

void CSjtestDlg::OnBnClickedButtonDelete()
{
// TODO: 在此添加控件通知处理程序代码
int indx = m_Grid.GetSelectionMark() ;//选中序号
CString str = m_Grid.GetItemText(indx,1) ;
if(indx < 0)
return ;
CString info ;
for(int i = 0 ; i < 4 ; i++){
info += m_Grid.GetItemText(indx,i) ;
info += _T("\n") ;
}
if(AfxMessageBox(info,MB_OKCANCEL,MB_ICONQUESTION) != IDOK)
return ;
ADOConn m_adoconn ;
m_adoconn.OnInitADOConn() ;
CString sql ;
sql.Format(_T("Delete from employees where 员工姓名 = '%s' "),str) ;
m_adoconn.ExecuteSQL((_bstr_t)sql) ;
m_Grid.DeleteAllItems() ;
sql.Format(_T("select * from employees order by 员工编号 desc")) ;
m_adoconn.m_pRecordset = m_adoconn.GetRecordSet((_bstr_t)sql) ; while(! m_adoconn.m_pRecordset->adoEOF)
{
m_Grid.InsertItem(0,_T(""));
m_Grid.SetItemText(0,0,(_bstr_t)m_adoconn.m_pRecordset->GetCollect(_T("员工编号")));
m_Grid.SetItemText(0,1,(_bstr_t)m_adoconn.m_pRecordset->GetCollect(_T("员工姓名")));
m_Grid.SetItemText(0,2,(_bstr_t)m_adoconn.m_pRecordset->GetCollect(_T("所属部门")));
m_Grid.SetItemText(0,3,(_bstr_t)m_adoconn.m_pRecordset->GetCollect(_T("基本工资")));
//将记录集指针移动到下一条记录
m_adoconn.m_pRecordset->MoveNext();
} m_adoconn.ExitConnect() ;
}

  ADO数据库操作

void DigAdd::OnBnClickedOk(){
// TODO: 在此添加控件通知处理程序代码
UpdateData() ;
CString sql ,x; ADOConn m_Adocon ;
m_Adocon.OnInitADOConn() ;
sql.Format(_T("select * from employees order by 员工编号 desc")) ;
m_Adocon.m_pRecordset = m_Adocon.GetRecordSet((_bstr_t)sql) ;
int Size = 0 ;
while(! m_Adocon.m_pRecordset->adoEOF){
Size++ ;
m_Adocon.m_pRecordset->MoveNext() ;
}
x.Format(_T("%d"),Size) ;
sql.Format(_T(" insert into employees (员工编号 ,员工姓名 , 所属部门 , 基本工资) \
values('%d' , '%s', '%s', '%s') "),1 + Size , this->m_name , this->m_bumen , this->m_money);
m_Adocon.ExecuteSQL((_bstr_t)sql) ;
MessageBox(x) ;
m_Adocon.ExitConnect() ;
CDialog::OnOK();
}