本例采用Microsoft SQL2008建立的一个数据库表
/**
**链接数据库操作
**/
在stdafx.h的头文件中加入
#import "C:\Program Files\Common Files\System\ado\msado15.dll" no_namespace\
rename("EOF","_EOF")rename("BOF","__BOF") //导入ADO动态链接库
就好啦!
/** * 初始化 OLE 库 */ if (!AfxOleInit()) { AfxMessageBox("");//IDP_OLE_INIT_FAILED return FALSE; } try { m_pConnection.CreateInstance("ADODB.Connection"); //创建连接对象实例 //_bstr_t strConnect="Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=Restaurant;Data Source=(教师机\\SQLEXPRESS)"; _bstr_t strConnect="driver={SQL Server};Server=你的服务器登陆名;DATABASE=Test;UID=sa;PWD=你的登陆密码"; m_pConnection->Open(strConnect,"","",adModeUnknown); //打开数据库 } catch (_com_error e) //捕捉错误 { AfxMessageBox(e.Description()); //弹出错误 }catch(...) { AfxMessageBox("数据库初始化错误,程序将关闭!"); return FALSE; }
/**
*添加数据
*/
try { m_pRecordset->AddNew();//添加新行 m_pRecordset->PutCollect("num",(_bstr_t)mySheet.m_Propper_PersonMessages.m_strNum); m_pRecordset->PutCollect("name",(_bstr_t)mySheet.m_Propper_PersonMessages.m_strName); m_pRecordset->PutCollect("age", (_bstr_t)mySheet.m_Propper_PersonMessages.m_strAge); m_pRecordset->PutCollect("sex",(_bstr_t)mySheet.m_Propper_PersonMessages.m_strSex); m_pRecordset->PutCollect("major",(_bstr_t)mySheet.m_Propper_PersonMessages.m_strMajor); m_pRecordset->PutCollect("classes",(_bstr_t)mySheet.m_Propper_PersonMessages.m_strClasses); // m_pRecordset->PutCollect("sex",(_bstr_t)mySheet.m_Propper_PersonMessages.m_strSex); m_pRecordset->Update(); // m_AdoCon.ExitConnect(); } catch(_com_error e) { AfxMessageBox(e.Description()); return; } AfxMessageBox(_T("插入成功!")); ///////////////////更新显示表//////////////////////////////////////// this->SetColumnStyle(); sql.Format(_T("select * from student order by num desc")); m_List.DeleteAllItems(); m_pRecordset.CreateInstance(__uuidof(Recordset));//创建记录集对象实例 m_pRecordset->Open(_bstr_t(sql),m_pConnection.GetInterfacePtr(), adOpenDynamic,adLockOptimistic,adCmdText);//执行SQL得到记录集 while(!m_pRecordset->_EOF)//判断记录不为空!!!! { this->InsertItem_List(); }
/**
*删除数据
*/
POSITION pos; pos = m_List.GetFirstSelectedItemPosition(); try { //m_pRecord->Move((long)0,(long)0); m_pRecordset->MoveLast(); //m_pRecordset->Move((long)pos,_variant_t(vtMissing)); m_pRecordset->Delete(adAffectCurrent); m_pRecordset->Update(); //m_AdoConn.ExitConnect(); } catch(...) { MessageBox("操作失败"); return; } MessageBox("删除成功."); ///////////////////更新显示表//////////////////////////////////////// this->SetColumnStyle(); sql.Format(_T("select * from student order by num desc")); m_List.DeleteAllItems(); m_pRecordset.CreateInstance(__uuidof(Recordset));//创建记录集对象实例 m_pRecordset->Open(_bstr_t(sql),m_pConnection.GetInterfacePtr(), adOpenDynamic,adLockOptimistic,adCmdText);//执行SQL得到记录集 while(!m_pRecordset->_EOF)//判断记录不为空!!!! { this->InsertItem_List(); }
/**********************************向列表插入数据*****************************/ void CPage_PM_S::InsertItem_List() { m_List.InsertItem(0,""); //向列表视图控件中插入行 //向列表视图控件中插入列 m_List.SetItemText(0,0,(char *)(_bstr_t)m_pRecordset->GetCollect("num")); m_List.SetItemText(0,1,(char *)(_bstr_t)m_pRecordset->GetCollect("name")); m_List.SetItemText(0,2,(char *)(_bstr_t)m_pRecordset->GetCollect("age")); m_List.SetItemText(0,3,(char *)(_bstr_t)m_pRecordset->GetCollect("sex")); m_List.SetItemText(0,4,(char *)(_bstr_t)m_pRecordset->GetCollect("major")); m_List.SetItemText(0,5,(char *)(_bstr_t)m_pRecordset->GetCollect("classes")); m_pRecordset->MoveNext(); //将记录集指针移动到下一条记录 }