本例采用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(,""); //向列表视图控件中插入行
//向列表视图控件中插入列
m_List.SetItemText(,,(char *)(_bstr_t)m_pRecordset->GetCollect("num"));
m_List.SetItemText(,,(char *)(_bstr_t)m_pRecordset->GetCollect("name"));
m_List.SetItemText(,,(char *)(_bstr_t)m_pRecordset->GetCollect("age"));
m_List.SetItemText(,,(char *)(_bstr_t)m_pRecordset->GetCollect("sex"));
m_List.SetItemText(,,(char *)(_bstr_t)m_pRecordset->GetCollect("major"));
m_List.SetItemText(,,(char *)(_bstr_t)m_pRecordset->GetCollect("classes")); m_pRecordset->MoveNext(); //将记录集指针移动到下一条记录 }

此外还有一种是从控件光标上对数据的删除,但是数据库的内容是不变的,只是显示在系统List控件上的对应光标会在视图上删除。

具体代码是这样的:

POSITION pos;
     
        int nIndex;

for(;pos=m_List.GetFirstSelectedItemPosition();)
            {
              nIndex=m_List.GetNextSelectedItem(pos);
              m_List.DeleteItem(nIndex);//删除光标对应的数
            }

此类删除,数据是不会丢失的,它只是表面上删除,但当数据刷新后,我们会发现它没有真正的删除!

这种处理一般用在 对数据的查询上,我们大多数时候都是不用对数据进行直接操作,我们用此类删除数据,一般只是为了让我们更好的处理数据,美观数据,可以让我们对数据进行更好的分析。

///////////////////////////////////////////////////////////

区别于此类的数据删除,主要代码如下:

m_pRecordset->MoveFirst();
         m_pRecordset->Delete(adAffectCurrent);
         m_pRecordset->Update();

这类数据删除是从数据上,或者说从根本上对数据展开删除!数据会永久丢失的。

最新文章

  1. es6 import export 与 node 中的module.exports exports
  2. UDS帧传输
  3. C#_使用SMTP发送邮件
  4. hive函数参考手册
  5. 2016年12月16日 星期五 --出埃及记 Exodus 21:11
  6. bzoj 1845: [Cqoi2005] 三角形面积并 扫描线
  7. hdu 4063 Aircraft(计算几何+最短路)
  8. C#多线程的用法7-线程间的协作ManualResetEvent
  9. Windows任务计划程序起始于参数自动修改
  10. React设计思想
  11. YARN集群的mapreduce测试(一)
  12. CentOS安装和配置FTP
  13. 使用Via浏览器+ADM下载器突破百度网盘下载限速
  14. UVALive - 7139(差分+模拟)
  15. db2修改表结构,增加列,删除列等
  16. STL_map.修改删除
  17. if __name__ == '__main__的理解
  18. 用sql + Ado设置access的字段的默认值
  19. HTML5 Canvas ( 图形变换矩阵 ) transform, setTransform
  20. Jmeter 如何让变量中包含变量

热门文章

  1. c# 1-2+3-4.....求和
  2. 异常测试之Socket网络异常
  3. JSOI2010 满汉全席
  4. OpenSL的使用
  5. python学习笔记07-元组 字典
  6. eclipse web开发插件安装
  7. 剑指offer五十之数组中重复的数字
  8. 启动Storm各个后台进程
  9. Spring Security构建Rest服务-1400-授权
  10. gcc 编译问题