WEB开发网
开发学院软件开发VC VC中用DAO实现树型控件的数据库访问 阅读

VC中用DAO实现树型控件的数据库访问

 2009-06-08 20:07:55 来源:WEB开发网   
核心提示: 6、使用Class Wizard向导在CmainFrame类中加入OnCreateClient()函数 、在CshengViewp类中加入OnTimer()、OnInitialUpdate()等函数;7、添加程序代码,编译运行程序,VC中用DAO实现树型控件的数据库访问(6),三、 程序代

6、使用Class Wizard向导在CmainFrame类中加入OnCreateClient()函数 、在CshengViewp类中加入OnTimer()、OnInitialUpdate()等函数;

7、添加程序代码,编译运行程序。

三、 程序代码

////////////////////////////////////////////////// ShengSet.h : header file
#if !defined(AFX_SHENGSET_H__2B60B60D_A4AB_46AC_9B71_D31623CD0C87__INCLUDED_)
#define AFX_SHENGSET_H__2B60B60D_A4AB_46AC_9B71_D31623CD0C87__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
class CShengSet : public CDaoRecordset
{
 public:
  CShengSet(CDaoDatabase* pDatabase = NULL);
  DECLARE_DYNAMIC(CShengSet)
  // Field/Param Data
  //{{AFX_FIELD(CShengSet, CDaoRecordset)
   long m_ID;
   CString m_column1;
   CString m_column2;
   long m_column3;
   CString m_column4;
   CString m_column5;
  //}}AFX_FIELD
  // Overrides
  // ClassWizard generated virtual function overrides
  //{{AFX_VIRTUAL(CShengSet)
   public:
    virtual CString GetDefaultDBName(); // Default database name
    virtual CString GetDefaultSQL(); // Default SQL for Recordset
    virtual void DoFieldExchange(CDaoFieldExchange* pFX); // RFX support
  //}}AFX_VIRTUAL
  // Implementation
  #ifdef _DEBUG
   virtual void AssertValid() const;
   virtual void Dump(CDumpContext& dc) const;
  #endif
};
#endif
///////////////////////////////// ShengSet.cpp : implementation file
#include "stdafx.h"
#include "Ppp.h"
#include "ShengSet.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
IMPLEMENT_DYNAMIC(CShengSet, CDaoRecordset)

CShengSet::CShengSet(CDaoDatabase* pdb)
: CDaoRecordset(pdb)
{
 //{{AFX_FIELD_INIT(CShengSet)
  m_ID = 0;
  m_column1 = _T("");
  m_column2 = _T("");
  m_column3 = 0;
  m_column4 = _T("");
  m_column5 = _T("");
  m_nFields = 6;
 //}}AFX_FIELD_INIT
 m_nDefaultType = dbOpenDynaset;
}

CString CShengSet::GetDefaultDBName()
{
 return _T("全国.mdb");
}

CString CShengSet::GetDefaultSQL()
{
 return _T("[省]");
}

void CShengSet::DoFieldExchange(CDaoFieldExchange* pFX)
{
 //{{AFX_FIELD_MAP(CShengSet)
  pFX->SetFieldType(CDaoFieldExchange::outputColumn);
  DFX_Long(pFX, _T("[ID]"), m_ID);
  DFX_Text(pFX, _T("[省名称]"), m_column1);
  DFX_Text(pFX, _T("[省代码]"), m_column2);
  DFX_Long(pFX, _T("[省人口]"), m_column3);
  DFX_Text(pFX, _T("[省面积]"), m_column4);
  DFX_Text(pFX, _T("[省简介]"), m_column5);
 //}}AFX_FIELD_MAP
}

#ifdef _DEBUG
void CShengSet::AssertValid() const
{
 CDaoRecordset::AssertValid();
}

void CShengSet::Dump(CDumpContext& dc) const
{
 CDaoRecordset::Dump(dc);
}
#endif //_DEBUG

///////////////////////////// PppView.h : interface of the CPppView class
#if !defined(AFX_PPPVIEW_H__8635E57E_3AFE_447A_8CCB_7B1B97A1BAC3__INCLUDED_)
#define AFX_PPPVIEW_H__8635E57E_3AFE_447A_8CCB_7B1B97A1BAC3__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
#include"PppDoc.h"
#include"ShengSet.h"
class CPppView : public CFormView
{
 protected: // create from serialization only
  CPppView();
  DECLARE_DYNCREATE(CPppView)
 public:
  //{{AFX_DATA(CPppView)
   enum { IDD = IDD_PPP_FORM };
   CTreeCtrl m_treectrl;
  //}}AFX_DATA
  // Attributes
 public:
  CPppDoc* GetDocument();
 public:
  CShengSet *m_ShengSet;
  // Operations
 public:
  // Overrides
  // ClassWizard generated virtual function overrides
  //{{AFX_VIRTUAL(CPppView)
  public:
   virtual BOOL PreCreateWindow(CREATESTRUCT& cs);
  protected:
   virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
   virtual void OnInitialUpdate(); // called first time after construct
   virtual BOOL OnPreparePrinting(CPrintInfo* pInfo);
   virtual void OnBeginPrinting(CDC* pDC, CPrintInfo* pInfo);
   virtual void OnEndPrinting(CDC* pDC, CPrintInfo* pInfo);
   virtual void OnPrint(CDC* pDC, CPrintInfo* pInfo);
  //}}AFX_VIRTUAL
 // Implementation
 public:
  virtual ~CPppView();
  #ifdef _DEBUG
   virtual void AssertValid() const;
   virtual void Dump(CDumpContext& dc) const;
  #endif
 protected:
  // Generated message map functions
 protected:
  //{{AFX_MSG(CPppView)
    afx_msg void OnSelchangingTree1(NMHDR* pNMHDR, LRESULT* pResult);
  //}}AFX_MSG
 DECLARE_MESSAGE_MAP()
};
#ifndef _DEBUG // debug version in PppView.cpp
inline CPppDoc* CPppView::GetDocument()
{ return (CPppDoc*)m_pDocument; }
#endif
#endif

////////////////// PppView.cpp : implementation of the CPppView class
#include "stdafx.h"
#include "Ppp.h"
#include "PppDoc.h"
#include "PppView.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
IMPLEMENT_DYNCREATE(CPppView, CFormView)
BEGIN_MESSAGE_MAP(CPppView, CFormView)
//{{AFX_MSG_MAP(CPppView)
 ON_NOTIFY(TVN_SELCHANGING, IDC_TREE1, OnSelchangingTree1)
//}}AFX_MSG_MAP
// Standard printing commands
ON_COMMAND(ID_FILE_PRINT, CFormView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_DIRECT, CFormView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_PREVIEW, CFormView::OnFilePrintPreview)
END_MESSAGE_MAP()

CPppView::CPppView()
: CFormView(CPppView::IDD)
{}

CPppView::~CPppView()
{}

void CPppView::DoDataExchange(CDataExchange* pDX)
{
 CFormView::DoDataExchange(pDX);
 //{{AFX_DATA_MAP(CPppView)
  DDX_Control(pDX, IDC_TREE1, m_treectrl);
 //}}AFX_DATA_MAP
}

BOOL CPppView::PreCreateWindow(CREATESTRUCT& cs)
{
 // TODO: Modify the Window class or styles here by modifying
 // the CREATESTRUCT cs
 return CFormView::PreCreateWindow(cs);
}

void CPppView::OnInitialUpdate()
{
 CFormView::OnInitialUpdate();
 //GetParentFrame()->RecalcLayout();
 //ResizeParentToFit();
 CPppDoc* pDoc = (CPppDoc*)GetDocument();
 m_ShengSet=&pDoc->m_ShengSet;
 TV_INSERTSTRUCT tvinsert;
 tvinsert.hParent = NULL;
 tvinsert.item.mask = TVIF_TEXT;
 tvinsert.item.pszText = "全国";
 HTREEITEM hDad = m_treectrl.InsertItem(&tvinsert);
 if (m_ShengSet->IsOpen())
  m_ShengSet->Close();
  m_ShengSet->Open();
  m_ShengSet->MoveFirst();
  CString c1;
  char buff[80];
 while(!m_ShengSet->IsEOF())
 {
  c1=m_ShengSet->m_column1;
  wsprintf(buff,"%s",c1);
  tvinsert.item.pszText = buff;
  tvinsert.hParent = hDad;
  m_treectrl.InsertItem(&tvinsert);
  m_ShengSet->MoveNext();
 }
}

BOOL CPppView::OnPreparePrinting(CPrintInfo* pInfo)
{
 // default preparation
  return DoPreparePrinting(pInfo);
}

void CPppView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
 // TODO: add extra initialization before printing
}

void CPppView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
 // TODO: add cleanup after printing
}

void CPppView::OnPrint(CDC* pDC, CPrintInfo* /*pInfo*/)
{
 // TODO: add customized printing code here
}

#ifdef _DEBUG
void CPppView::AssertValid() const
{
 CFormView::AssertValid();
}

void CPppView::Dump(CDumpContext& dc) const
{
 CFormView::Dump(dc);
}

CPppDoc* CPppView::GetDocument() // non-debug version is inline
{
 ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CPppDoc)));
 return (CPppDoc*)m_pDocument;
}
#endif //_DEBUG

void CPppView::OnSelchangingTree1(NMHDR* pNMHDR, LRESULT* pResult)
{
 NM_TREEVIEW* pNMTreeView = (NM_TREEVIEW*)pNMHDR;
 // TODO: Add your control notification handler code here
 CTreeCtrl* pTree2 = (CTreeCtrl*) GetDlgItem(IDC_TREE1);
 HTREEITEM hSelected = pNMTreeView->itemNew.hItem;
 CPppDoc* pDoc = (CPppDoc*)GetDocument();
 CString hParentName;
 HTREEITEM hParent=pTree2->GetParentItem(hSelected);
 if(hSelected!=NULL)
 {
  pDoc->Name=pTree2->GetItemText(hSelected);
 }
 *pResult = 0;
}

////////////////////////////////////////////////////////////// CMainFrame message handlers
BOOL CMainFrame::OnCreateClient(LPCREATESTRUCT lpcs, CCreateContext* pContext)
{
 // TODO: Add your specialized code here and/or call the base class
 if (!m_wndSpMain.CreateStatic(this, 1, 2))
 {
  TRACE0("Failed to create splitter windown");
  return FALSE;
 }
 // Get the client rect first for calc left pane size
 // create the left tree view first.
 if (!m_wndSpMain.CreateView(0, 0, RUNTIME_CLASS(CPppView),
 CSize(200, 200), pContext))
 {
  TRACE0("Failed to create left pane viewn");
  return FALSE;
 }
 // The right pane is a frame which and contain several different views.
 if (!m_wndSpMain.CreateView(0, 1,RUNTIME_CLASS(CShengView), CSize(100,100), pContext))
 {
  TRACE0("Failed to create right pane framen");
  return FALSE;
 }
 return TRUE;
}

四、小结

本实例介绍了DAO编程的基本知识,并利用不同的视,巧妙地实现了树型控件与数据库相关联,实现了数据库的灵活操作。如果读者朋友需要实现类似的处理,该实例对于启迪思路一定会有所帮助。

上一页  1 2 3 4 5 6 

Tags:VC 中用 DAO

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接