// testView.cpp : implementation of the CTestView class // #include "stdafx.h" #include "test.h" #include "testSet.h" #include "testDoc.h" #include "testView.h" #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif ///////////////////////////////////////////////////////////////////////////// // CTestView IMPLEMENT_DYNCREATE(CTestView, CRecordView) BEGIN_MESSAGE_MAP(CTestView, CRecordView) //{{AFX_MSG_MAP(CTestView) ON_BN_CLICKED(IDC_ADD, OnAdd) ON_BN_CLICKED(IDC_DEL, OnDel) ON_BN_CLICKED(IDC_QU, OnQu) //}}AFX_MSG_MAP // Standard printing commands ON_COMMAND(ID_FILE_PRINT, CRecordView::OnFilePrint) ON_COMMAND(ID_FILE_PRINT_DIRECT, CRecordView::OnFilePrint) ON_COMMAND(ID_FILE_PRINT_PREVIEW, CRecordView::OnFilePrintPreview) END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// // CTestView construction/destruction CTestView::CTestView() : CRecordView(CTestView::IDD) { //{{AFX_DATA_INIT(CTestView) m_pSet = NULL; m_na = _T(""); //}}AFX_DATA_INIT // TODO: add construction code here } CTestView::~CTestView() { } void CTestView::DoDataExchange(CDataExchange* pDX) { CRecordView::DoDataExchange(pDX); //{{AFX_DATA_MAP(CTestView) DDX_Text(pDX, IDC_name,m_pSet->m_column1); DDX_Text(pDX, IDC_sex,m_pSet->m_column2); DDX_Text(pDX, IDC_age,m_pSet->m_column3); DDX_Text(pDX, IDC_name2, m_na); //}}AFX_DATA_MAP } BOOL CTestView::PreCreateWindow(CREATESTRUCT& cs) { // TODO: Modify the Window class or styles here by modifying // the CREATESTRUCT cs return CRecordView::PreCreateWindow(cs); } void CTestView::OnInitialUpdate() { m_pSet = &GetDocument()->m_testSet; CRecordView::OnInitialUpdate(); GetParentFrame()->RecalcLayout(); ResizeParentToFit(); } ///////////////////////////////////////////////////////////////////////////// // CTestView printing BOOL CTestView::OnPreparePrinting(CPrintInfo* pInfo) { // default preparation return DoPreparePrinting(pInfo); } void CTestView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/) { // TODO: add extra initialization before printing } void CTestView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/) { // TODO: add cleanup after printing } ///////////////////////////////////////////////////////////////////////////// // CTestView diagnostics #ifdef _DEBUG void CTestView::AssertValid() const { CRecordView::AssertValid(); } void CTestView::Dump(CDumpContext& dc) const { CRecordView::Dump(dc); } CTestDoc* CTestView::GetDocument() // non-debug version is inline { ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CTestDoc))); return (CTestDoc*)m_pDocument; } #endif //_DEBUG ///////////////////////////////////////////////////////////////////////////// // CTestView database support CRecordset* CTestView::OnGetRecordset() { return m_pSet; } ///////////////////////////////////////////////////////////////////////////// // CTestView message handlers void CTestView::OnAdd() { // TODO: Add your control notification handler code here CAddNew dlg; //定义对象 CString str; if(IDOK==dlg.DoModal()) { try{ CRecordsetAdd m_pdatabase; if(m_pdatabase.IsOpen()) m_pdatabase.Close(); m_pdatabase.Open(CRecordset::snapshot,NULL,CRecordset::none); m_pdatabase.AddNew(); //添加记录 m_pdatabase.m_column1=dlg.m_name; m_pdatabase.m_column2=dlg.m_sex; m_pdatabase.m_column3=dlg.m_age; if(m_pdatabase.CanUpdate()) m_pdatabase.Update(); //进行更新 m_pSet->Requery(); m_pdatabase.Close(); if(!m_pSet->IsEOF()) m_pSet->MoveNext(); //下一条记录 } catch(CDBException* e) //捕捉异常 { e->ReportError(); return; } str.Format("添加记录[ %s ]成功",dlg.m_name); MessageBox(str,NULL,MB_OK|MB_ICONINFORMATION);//弹出消息框 } } void CTestView::OnDel() { // TODO: Add your control notification handler code here try{ CString str; CRecordsetAdd m_pdatabase; if(m_pdatabase.IsOpen()) m_pdatabase.Close(); str.Format("%s",m_pSet->m_column1); m_pdatabase.m_strFilter.Format("姓名='%s'",str); m_pdatabase.Open(CRecordset::snapshot,NULL,CRecordset::none); m_pdatabase.Delete(); //删除记录 if(!m_pdatabase.IsEOF()) m_pdatabase.MoveLast(); //最后一条记录 else m_pdatabase.SetFieldNull(NULL); if(m_pdatabase.IsOpen()) m_pdatabase.Close(); m_pSet->Requery(); //如果此处还是写m_pSet->m_name则错误,因为已经重新连接 str.Format("删除记录 ["+m_pdatabase.m_column1+"] 成功!{}-..-{}"); MessageBox(str,NULL,MB_OK|MB_ICONINFORMATION); } catch(CDBException* e) //捕捉异常 { e->ReportError(); return; } } void CTestView::OnQu() { // TODO: Add your control notification handler code here UpdateData(TRUE); //更新数据 CString col="姓名"; m_pSet->Close(); m_pSet->m_strFilter=col+"='"+m_na+"'"; //按字段进行查询 m_pSet->Open(); UpdateData(FALSE); //更新数据,显示结果 }