zoukankan      html  css  js  c++  java
  • MFC数据库操作

    本例采用Microsoft SQL2008建立的一个数据库表

    /**
    **链接数据库操作
    **/

    在stdafx.h的头文件中加入

    #import "C:Program FilesCommon FilesSystemadomsado15.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();                        //将记录集指针移动到下一条记录
    
    }

    此外还有一种是从控件光标上对数据的删除,但是数据库的内容是不变的,只是显示在系统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();

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

  • 相关阅读:
    Xamarin Forms 的一个小事故 dotNET界面
    求极限求无限数相加算法
    C# 获取鼠标位置
    入门级XML学习(三)
    DateTime 获取时间
    《人工智能的未来》经典语录
    VS编辑器 高亮消失如何恢复
    C# 获取桌面
    导出EXECL 2003正常 2007 不正常问题
    css 控制div高度自适应浏览器的高度
  • 原文地址:https://www.cnblogs.com/Ricezhang/p/3959419.html
Copyright © 2011-2022 走看看