暑期实习17之数据库的引入

来源:互联网 发布:滨州行知中学 听风 编辑:程序博客网 时间:2024/06/10 05:47

把数据库与文档联系起来,并实现通过编写代码进行数据的存入、显示、查找、修改、删除等的功能。

首先要新建一个Studentshujvbiao的类。

//Studentshijvbiao.h#if !defined(AFX_STUDENTSHUJVBIAO_H__5156FC39_F34E_4428_82BC_9BFA9C3DC022__INCLUDED_)#define AFX_STUDENTSHUJVBIAO_H__5156FC39_F34E_4428_82BC_9BFA9C3DC022__INCLUDED_#if _MSC_VER > 1000#pragma once#endif // _MSC_VER > 1000// Studentshujvbiao.h : header file///////////////////////////////////////////////////////////////////////////////// Studentshujvbiao recordset#include<afxdb.h>class Studentshujvbiao : public CRecordset{public:Studentshujvbiao(CDatabase* pDatabase = NULL);DECLARE_DYNAMIC(Studentshujvbiao)// Field/Param Data//{{AFX_FIELD(Studentshujvbiao, CRecordset)CStringm_ID;CStringm_name;CStringm_chengji;//}}AFX_FIELD// Overrides// ClassWizard generated virtual function overrides//{{AFX_VIRTUAL(Studentshujvbiao)public:virtual CString GetDefaultConnect();    // Default connection stringvirtual CString GetDefaultSQL();    // Default SQL for Recordsetvirtual void DoFieldExchange(CFieldExchange* pFX);  // RFX support//}}AFX_VIRTUAL// Implementation#ifdef _DEBUGvirtual void AssertValid() const;virtual void Dump(CDumpContext& dc) const;#endif};//{{AFX_INSERT_LOCATION}}// Microsoft Visual C++ will insert additional declarations immediately before the previous line.#endif // !defined(AFX_STUDENTSHUJVBIAO_H__5156FC39_F34E_4428_82BC_9BFA9C3DC022__INCLUDED_)
//Studentshujvbiao.cpp
#include "stdafx.h"#include "7.h"#include "Studentshujvbiao.h"#ifdef _DEBUG#define new DEBUG_NEW#undef THIS_FILEstatic char THIS_FILE[] = __FILE__;#endif/////////////////////////////////////////////////////////////////////////////// StudentshujvbiaoIMPLEMENT_DYNAMIC(Studentshujvbiao, CRecordset)Studentshujvbiao::Studentshujvbiao(CDatabase* pdb): CRecordset(pdb){//{{AFX_FIELD_INIT(Studentshujvbiao)m_ID = _T("");m_name = _T("");m_chengji = _T("");m_nFields = 3;//}}AFX_FIELD_INITm_nDefaultType = snapshot;}CString Studentshujvbiao::GetDefaultConnect(){return _T("ODBC;DSN=Mysource");}CString Studentshujvbiao::GetDefaultSQL(){return _T("[studenttable]");}void Studentshujvbiao::DoFieldExchange(CFieldExchange* pFX){//{{AFX_FIELD_MAP(Studentshujvbiao)pFX->SetFieldType(CFieldExchange::outputColumn);RFX_Text(pFX, _T("[ID]"), m_ID);RFX_Text(pFX, _T("[name]"), m_name);RFX_Text(pFX, _T("[chengji]"), m_chengji);//}}AFX_FIELD_MAP}/////////////////////////////////////////////////////////////////////////////// Studentshujvbiao diagnostics#ifdef _DEBUGvoid Studentshujvbiao::AssertValid() const{CRecordset::AssertValid();}void Studentshujvbiao::Dump(CDumpContext& dc) const{CRecordset::Dump(dc);}#endif //_DEBUG
//view.h#if !defined(AFX_7VIEW_H__A7AFAC7A_5458_4331_BDD8_35946C1AA08D__INCLUDED_)#define AFX_7VIEW_H__A7AFAC7A_5458_4331_BDD8_35946C1AA08D__INCLUDED_#if _MSC_VER > 1000#pragma once#endif // _MSC_VER > 1000#include "Studentshujvbiao.h"class CMy7View : public CView{protected: // create from serialization onlyCMy7View();DECLARE_DYNCREATE(CMy7View)// Attributespublic:CMy7Doc* GetDocument();// Operationspublic:// Overrides// ClassWizard generated virtual function overrides//{{AFX_VIRTUAL(CMy7View)public:virtual void OnDraw(CDC* pDC);  // overridden to draw this viewvirtual BOOL PreCreateWindow(CREATESTRUCT& cs);protected:virtual BOOL OnPreparePrinting(CPrintInfo* pInfo);virtual void OnBeginPrinting(CDC* pDC, CPrintInfo* pInfo);virtual void OnEndPrinting(CDC* pDC, CPrintInfo* pInfo);//}}AFX_VIRTUAL// Implementationpublic:virtual ~CMy7View();#ifdef _DEBUGvirtual void AssertValid() const;virtual void Dump(CDumpContext& dc) const;#endifprotected:// Generated message map functionsprotected://{{AFX_MSG(CMy7View)afx_msg void OnLButtonDown(UINT nFlags, CPoint point);afx_msg void OnRButtonDown(UINT nFlags, CPoint point);//}}AFX_MSGDECLARE_MESSAGE_MAP()};#ifndef _DEBUG  // debug version in 7View.cppinline CMy7Doc* CMy7View::GetDocument()   { return (CMy7Doc*)m_pDocument; }#endif
//view.cpp#include "stdafx.h"#include "7.h"#include "7Doc.h"#include "7View.h"#ifdef _DEBUG#define new DEBUG_NEW#undef THIS_FILEstatic char THIS_FILE[] = __FILE__;#endif/////////////////////////////////////////////////////////////////////////////// CMy7ViewIMPLEMENT_DYNCREATE(CMy7View, CView)BEGIN_MESSAGE_MAP(CMy7View, CView)//{{AFX_MSG_MAP(CMy7View)ON_WM_LBUTTONDOWN()ON_WM_RBUTTONDOWN()//}}AFX_MSG_MAP// Standard printing commandsON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint)ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)END_MESSAGE_MAP()/////////////////////////////////////////////////////////////////////////////// CMy7View construction/destructionCMy7View::CMy7View(){// TODO: add construction code here}CMy7View::~CMy7View(){}BOOL CMy7View::PreCreateWindow(CREATESTRUCT& cs){// TODO: Modify the Window class or styles here by modifying//  the CREATESTRUCT csreturn CView::PreCreateWindow(cs);}/////////////////////////////////////////////////////////////////////////////// CMy7View drawingvoid CMy7View::OnDraw(CDC* pDC){CMy7Doc* pDoc = GetDocument();ASSERT_VALID(pDoc);}/////////////////////////////////////////////////////////////////////////////// CMy7View printingBOOL CMy7View::OnPreparePrinting(CPrintInfo* pInfo){// default preparationreturn DoPreparePrinting(pInfo);}void CMy7View::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/){// TODO: add extra initialization before printing}void CMy7View::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/){// TODO: add cleanup after printing}/////////////////////////////////////////////////////////////////////////////// CMy7View diagnostics#ifdef _DEBUGvoid CMy7View::AssertValid() const{CView::AssertValid();}void CMy7View::Dump(CDumpContext& dc) const{CView::Dump(dc);}CMy7Doc* CMy7View::GetDocument() // non-debug version is inline{ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CMy7Doc)));return (CMy7Doc*)m_pDocument;}#endif //_DEBUG/////////////////////////////////////////////////////////////////////////////// CMy7View message handlersvoid CMy7View::OnLButtonDown(UINT nFlags, CPoint point) //单击左键实现以下功能{// TODO: Add your message handler code here and/or call default    Studentshujvbiao s3;    //存数据s3.Open();s3.AddNew();s3.m_ID="003";s3.m_name="小tu";s3.m_chengji="100";if(!s3.Update()){MessageBox("error");}s3.Close();/*Studentshujvbiao s;        //修改数据s.Open();s.m_strFilter="ID='001'";s.Edit();s.m_name="小驴";s.Update();s.Close();*/CView::OnLButtonDown(nFlags, point);}void CMy7View::OnRButtonDown(UINT nFlags, CPoint point) //单击右键实现以下功能{// TODO: Add your message handler code here and/or call default/*Studentshujvbiao s2;      //显示数据s2.Open();CClientDC dc(this);CString str;int y=20;while(!s2.IsEOF()){   str.Format("ID:%s,name:%s,chengji:%s",s2.m_ID,s2.m_name,s2.m_chengji);       dc.TextOut(20,y,str);   y+=20;   s2.MoveNext();}s2.Close();*/    Studentshujvbiao st;         //查找数据int y=20;st.Open();CClientDC dc(this);st.m_strFilter="chengji='90'";while(!st.IsEOF()){              CString str;   str.Format("ID:%s,name:%s,chengji:%s",st.m_ID,st.m_name,st.m_chengji);       dc.TextOut(20,y,str);   y+=20;   st.MoveNext();}st.Close();    /*Studentshujvbiao s2;     //删除数据方法1s2.Open();while(!s2.IsEOF()){s2.Delete();s2.MoveNext();}s2.Close();*//*CDatabase db;         //删除数据方法2Studentshujvbiao s(&db);CString str="delete from studenttable";s.Open();db.ExecuteSQL(str);s.Close();*/CView::OnRButtonDown(nFlags, point);}


 

原创粉丝点击