MFC 简单数据库设计(1)

#pragma once
#include "afxcmn.h"
#include "afxwin.h"

//下面这个要加入,因为要用ADO
#import  "c:\program files\common files\system\ado\msado15.dll"  no_namespace   rename("EOF","adoEOF")

 

// CStudentDlg 对话框
class CStudentDlg : public CDialog
{
// 构造
public:
 CStudentDlg(CWnd* pParent = NULL); // 标准构造函数
       bool m_bOpen;

//下面两个是重点,一个是数据库的链接,一个是数据库的操作
   _ConnectionPtr m_pConnection;
   _RecordsetPtr m_pRecordset;

// 对话框数据
 enum { IDD = IDD_STUDENT_DIALOG };

 protected:
 virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV 支持


// 实现
protected:
 HICON m_hIcon;

 // 生成的消息映射函数
 virtual BOOL OnInitDialog();
 afx_msg void OnSysCommand(UINT nID, LPARAM lParam);
 afx_msg void OnPaint();
 afx_msg HCURSOR OnQueryDragIcon();
 DECLARE_MESSAGE_MAP()
public:
 CListCtrl m_ListInfo;
 CComboBox m_Sex;
 afx_msg void OnBnClickedButton1();


 bool OpenDBLink(void);
 void CloseDBLink(void);
 void LoadAllData(void);


};

 

实现:

 


void CStudentDlg::OnBnClickedButton1()
{
     this->OpenDBLink();
  this->LoadAllData();
}

 

bool CStudentDlg::OpenDBLink(void)
{
 if(m_bOpen)
 {
  this->CloseDBLink();
 }
 try{
  

  CoInitialize(NULL);
  //m_pConnection.CreateInstance(__uuidof(Connection));

  HRESULT hr;
   hr = m_pConnection.CreateInstance("ADODB.Connection");

  if(SUCCEEDED(hr))
  {
   hr=m_pConnection->Open("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=student.mdb","","",adModeUnknown);


   m_bOpen = true;
  }
  else
  {
   m_bOpen =false;
   return m_bOpen;
  }

 try
     { 
   m_pRecordset.CreateInstance("ADODB.Recordset");
         m_pRecordset->Open("SELECT * FROM student",      
         m_pConnection.GetInterfacePtr(), // 获取库接库的IDispatch指针
         adOpenDynamic,
         adLockOptimistic,
         adCmdText);
     }
     catch(_com_error *e)
     {
         AfxMessageBox(e->ErrorMessage());
         //cout<<e->ErrorMessage()<<endl;
     } 

  
     //m_pRecordset.CreateInstance("ADODB.Recordset");
  //m_pRecordset->Open("SELECT * FROM student",_variant_t((IDispatch*)m_pConnection,true),adOpenStatic,adLockOptimistic,adCmdText);
  
  if(!m_pRecordset->BOF)
  m_pRecordset->MoveFirst();
 }
catch(_com_error e)
{
       
 
       MessageBox("失败!"+e.Description());
    m_bOpen = false;
 
}


 

 return true;
}

void CStudentDlg::CloseDBLink(void)
{
  if(m_bOpen==false)
  return;
 m_pRecordset->Close();
  m_pConnection->Close();
}

void CStudentDlg::LoadAllData(void)
{
 if(m_bOpen==false)
  return;
 _variant_t value;
 COleDateTime time;
 CString name,sex;

 int age;

 m_ListInfo.DeleteAllItems();


 int index=0;

 _variant_t var;
     char *strID;
     try
     {
         if(!m_pRecordset->BOF)
             m_pRecordset->MoveFirst();
         else
         {
            // cout<<"表内数据为空"<<endl;
            // return 1;
         }
 
       
         // 读入库中各字段并加入列表框中
         while(!m_pRecordset->adoEOF)
         {
  
             var = m_pRecordset->GetCollect("NAME");
            

             m_pRecordset->MoveNext();
         }
     }
     catch(_com_error *e)
     {
         //cout<<e->ErrorMessage()<<endl;
     } 

  m_Sex.AddString(_T("未知"));
   m_Sex.AddString(_T("男"));
    m_Sex.AddString(_T("女"));
    m_Sex.SetCurSel(0);


  m_ListInfo.SetExtendedStyle(LVS_EX_GRIDLINES|LVS_EX_FULLROWSELECT|LVS_EX_HEADERDRAGDROP);
  m_ListInfo.InsertColumn(0,CString("姓名"),LVCFMT_CENTER,100);
  m_ListInfo.InsertColumn(1,CString("性别"),LVCFMT_LEFT,50);
  m_ListInfo.InsertColumn(2,CString("年龄"),LVCFMT_LEFT,50);
            if(!m_pRecordset->BOF)
             m_pRecordset->MoveFirst();
 while(!m_pRecordset->adoEOF )
 {
  value = m_pRecordset->GetCollect("NAME");
  if(value.vt != VT_NULL)
   name = value.bstrVal ;
  else
   name="";

  value=m_pRecordset->GetCollect("SEX");
  if(value.vt !=VT_NULL)
   sex=value.bstrVal ;
  else

   sex="NOO";

    value=m_pRecordset->GetCollect("AGE");
  if(value.vt !=VT_NULL)
   age=(int)value.llVal ;
  else

   age=25;

  m_ListInfo.InsertItem(index,name,0);
  m_ListInfo.SetItemText(index,1,sex);
  
       
  CString temp;
  temp.Format(_T("%d"),age);
  m_ListInfo.SetItemText(index,2,temp);

  index++;

  m_pRecordset->MoveNext();

 }

 

posted on 2012-03-10 19:50  Mayvar  阅读(457)  评论(0编辑  收藏  举报

导航