设计基于ODBC的数据库管理系统.doc_第1页
设计基于ODBC的数据库管理系统.doc_第2页
设计基于ODBC的数据库管理系统.doc_第3页
设计基于ODBC的数据库管理系统.doc_第4页
设计基于ODBC的数据库管理系统.doc_第5页
已阅读5页,还剩9页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、用VC+设计基于ODBC的数据库管理系统(4)二、编程步骤1、启动Visual C+6。0,生成一个单文档视图结构的应用程序,将该程序命名为”Enroll"。在MFC AppWizard的第一步选择Single document,在MFC AppWizard的第二步选择Database view without file support,然后点击Data Source。.按钮,在Database Options对话框中的ODBC组合框中选择Student Registration数据源,则会打开一个Select Database Table对话框,在该对话框中选择数据库中的Secti

2、on表按OK按钮退出,在MFC AppWizard的第六步中,将类CEnrollSet改名为CSectionSet,将类CEnrollView改名为CSectionForm;2、接下来的任务设计应用程序的界面(具体设置请参见原代码部分),并用ClassWizard把表单中的控件与记录集的域数据成员连接起来,以实现控件与当前记录的DDX数据交换。请读者按如下步骤操作:进入ClassWizard,选择Member Variables页并且选择CSectionForm类.在变量列表中双击IDC_CAPACITY项,则会显示Add Member Variable对话框。注意该对话框的Member va

3、riable name栏显示的是一个组合框,而不是平常看到的编辑框。在组合框的列表中选择m_pSet-m_Capacity并按OK按钮确认。其它控件依此类推。3、对于组合框控件IDC_COURSELIST项,除在Add Member Variable对话框的组合框中选择m_pSetm_CourseID外,还要再次双击IDC_COURSELIST,并为CSectionForm类加入一个名为m_ctlCourseList的CComboBox类成员。4、使用Class Wizard为IDC_COURSELIST组合框加入CBN_SELENDOK通知消息处理函数,函数名为OnSelendokCours

4、elist。该函数负责响应用户在组合框中选择的变化.5、接着使用ClassWizard为数据库中的Course表创建一个名为CCourseSet的记录集类;6、在CEnrollDoc类的定义中,紧接着m_sectionSet成员,加入下面一行:CCourseSet m_courseSet.这样CEnrollDoc就包含了两个记录集。由于CEnrollDoc类用到了CCourseSet类,所以要在所有含有include "EnrolDoc。h"语句的CPP文件中,在#include "EnrolDoc.h"语句的前面加上include "Cour

5、seSet。h”语句。7、在CSectionSet类的定义中,紧接着域数据成员,在"/AFX_FIELD”注释外加入下面一行CString m_strCourseIDParam;8、添加代码,编译运行程序.三、程序代码/ SectSet.h : interface of the CSectionSet classclass CSectionSet : public CRecordsetpublic:CSectionSet(CDatabase* pDatabase = NULL);DECLARE_DYNAMIC(CSectionSet)/ Field/Param Data/AFX_FI

6、ELD(CSectionSet, CRecordset)CString m_CourseID;CString m_SectionNo;CString m_InstructorID;CString m_RoomNo;CString m_Schedule;int m_Capacity;/AFX_FIELDCString m_strCourseIDParam;/ Overrides/ ClassWizard generated virtual function overrides/AFX_VIRTUAL(CSectionSet)public:virtual CString GetDefaultCon

7、nect(); / Default connection stringvirtual CString GetDefaultSQL(); / default SQL for Recordsetvirtual void DoFieldExchange(CFieldExchange pFX); / RFX support/AFX_VIRTUAL/ Implementationifdef _DEBUGvirtual void AssertValid() const;virtual void Dump(CDumpContext& dc) const;#endif ;/ SectSet。cpp :

8、 implementation of the CSectionSet class#include "stdafx。h"#include ”Enroll.h"include ”SectSet。h”#ifdef _DEBUGdefine new DEBUG_NEWundef THIS_FILEstatic char THIS_FILE = _FILE_;#endifIMPLEMENT_DYNAMIC(CSectionSet, CRecordset)CSectionSet:CSectionSet(CDatabase* pdb): CRecordset(pdb)/AFX_

9、FIELD_INIT(CSectionSet)m_CourseID = _T(”");m_SectionNo = _T(”");m_InstructorID = _T("”);m_RoomNo = _T("”);m_Schedule = _T("”);m_Capacity = 0;m_nFields = 6;/AFX_FIELD_INITm_nDefaultType = snapshot;m_nParams = 1;m_strCourseIDParam = "”;CString CSectionSet::GetDefaultConne

10、ct()return _T("ODBC;DSN=Student Registration”);CString CSectionSet:GetDefaultSQL()return _T(”Section");void CSectionSet:DoFieldExchange(CFieldExchange* pFX)/AFX_FIELD_MAP(CSectionSet)pFX>SetFieldType(CFieldExchange::outputColumn);RFX_Text(pFX, _T("CourseID"), m_CourseID);RFX_T

11、ext(pFX, _T("SectionNo”), m_SectionNo);RFX_Text(pFX, _T("InstructorID”), m_InstructorID);RFX_Text(pFX, _T(”RoomNo"), m_RoomNo);RFX_Text(pFX, _T("Schedule”), m_Schedule);RFX_Int(pFX, _T("Capacity"), m_Capacity);/AFX_FIELD_MAPpFXSetFieldType(CFieldExchange::param);RFX_Tex

12、t(pFX, "CourseIDParam", m_strCourseIDParam);#ifdef _DEBUGvoid CSectionSet:AssertValid() constCRecordset::AssertValid();void CSectionSet::Dump(CDumpContext& dc) constCRecordset::Dump(dc);endif /_DEBUG/ coursset。h : header fileclass CCourseSet : public CRecordsetpublic:CCourseSet(CDataba

13、se pDatabase = NULL);DECLARE_DYNAMIC(CCourseSet)/ Field/Param Data/AFX_FIELD(CCourseSet, CRecordset)CString m_CourseID;CString m_CourseTitle;int m_Hours;/AFX_FIELD/ Overrides/ ClassWizard generated virtual function overrides/AFX_VIRTUAL(CCourseSet)public:virtual CString GetDefaultConnect(); / Defaul

14、t 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;/ coursset。cpp : implementation file

15、#include ”stdafx。h"#include "enroll。h”include "coursset。h”ifdef _DEBUG#undef THIS_FILEstatic char BASED_CODE THIS_FILE = _FILE_;#endifIMPLEMENT_DYNAMIC(CCourseSet, CRecordset)CCourseSet::CCourseSet(CDatabase* pdb): CRecordset(pdb)/AFX_FIELD_INIT(CCourseSet)m_CourseID = _T(”");m_C

16、ourseTitle = _T(”);m_Hours = 0;m_nFields = 3;/AFX_FIELD_INITCString CCourseSet::GetDefaultConnect()return _T("ODBC;DSN=Student Registration;");CString CCourseSet:GetDefaultSQL()return _T("COURSE");void CCourseSet::DoFieldExchange(CFieldExchange* pFX)/AFX_FIELD_MAP(CCourseSet)pFX-

17、SetFieldType(CFieldExchange:outputColumn);RFX_Text(pFX, ”CourseID", m_CourseID);RFX_Text(pFX, "CourseTitle", m_CourseTitle);RFX_Int(pFX, ”Hours”, m_Hours);/AFX_FIELD_MAPifdef _DEBUGvoid CCourseSet::AssertValid() constCRecordset::AssertValid();void CCourseSet::Dump(CDumpContext dc) con

18、stCRecordset:Dump(dc);#endif /_DEBUG/ EnrolDoc。h : interface of the CEnrollDoc classclass CEnrollDoc : public CDocumentprotected: / create from serialization onlyCEnrollDoc();DECLARE_DYNCREATE(CEnrollDoc)/ Attributespublic:CSectionSet m_sectionSet;CCourseSet m_courseSet;/ Operationspublic:/ Override

19、s/ ClassWizard generated virtual function overrides/AFX_VIRTUAL(CEnrollDoc)public:virtual BOOL OnNewDocument();/AFX_VIRTUAL/ Implementationpublic:virtual CEnrollDoc();ifdef _DEBUGvirtual void AssertValid() const;virtual void Dump(CDumpContext& dc) const;#endifprotected:/ Generated message map fu

20、nctionsprotected:/AFX_MSG(CEnrollDoc)/ NOTE - the ClassWizard will add and remove member functions here./ DO NOT EDIT what you see in these blocks of generated code !/AFX_MSGDECLARE_MESSAGE_MAP();/ EnrolDoc。cpp : implementation of the CEnrollDoc class#include ”Stdafx.h"include "Enroll。h&qu

21、ot;#include "SectSet。h"include "CoursSet.h”#include ”EnrolDoc.h”ifdef _DEBUG#define new DEBUG_NEW#undef THIS_FILEstatic char THIS_FILE = _FILE_;endifIMPLEMENT_DYNCREATE(CEnrollDoc, CDocument)BEGIN_MESSAGE_MAP(CEnrollDoc, CDocument)/AFX_MSG_MAP(CEnrollDoc)/ NOTE - the ClassWizard will

22、add and remove mapping macros here。/ DO NOT EDIT what you see in these blocks of generated code!/AFX_MSG_MAPEND_MESSAGE_MAP()CEnrollDoc:CEnrollDoc()/ TODO: add onetime construction code hereCEnrollDoc::CEnrollDoc()BOOL CEnrollDoc:OnNewDocument()if (!CDocument:OnNewDocument())return FALSE;return TRUE

23、;#ifdef _DEBUGvoid CEnrollDoc:AssertValid() constCDocument:AssertValid();void CEnrollDoc::Dump(CDumpContext dc) constCDocument:Dump(dc);endif /_DEBUG/ SectForm。h : interface of the CSectionForm classclass CSectionSet;class CSectionForm : public CRecordViewprotected: / create from serialization onlyC

24、SectionForm();DECLARE_DYNCREATE(CSectionForm)public:/AFX_DATA(CSectionForm)enum IDD = IDD_ENROLL_FORM ;CEdit m_ctlSection;CComboBox m_ctlCourseList;CSectionSet* m_pSet;/AFX_DATA/ Attributespublic:CEnrollDoc* GetDocument();protected:BOOL m_bAddMode;/ Operationspublic:/ Overrides/ ClassWizard generate

25、d virtual function overrides/AFX_VIRTUAL(CSectionForm)public:virtual CRecordset OnGetRecordset();virtual BOOL PreCreateWindow(CREATESTRUCT& cs);virtual BOOL OnMove(UINT nIDMoveCommand);protected:virtual void DoDataExchange(CDataExchange* pDX); / DDX/DDV supportvirtual void OnInitialUpdate(); / c

26、alled first time after constructvirtual BOOL OnPreparePrinting(CPrintInfo pInfo);virtual void OnBeginPrinting(CDC* pDC, CPrintInfo pInfo);virtual void OnEndPrinting(CDC pDC, CPrintInfo* pInfo);/AFX_VIRTUAL/ Implementationpublic:virtual CSectionForm();ifdef _DEBUGvirtual void AssertValid() const;virt

27、ual void Dump(CDumpContext dc) const;#endifprotected:/ Generated message map functionsprotected:/AFX_MSG(CSectionForm)afx_msg void OnSelendokCourselist();afx_msg void OnRecordAdd();afx_msg void OnRecordDelete();afx_msg void OnRecordRefresh();/AFX_MSGDECLARE_MESSAGE_MAP();#ifndef _DEBUG / debug versi

28、on in SectForm.cppinline CEnrollDoc* CSectionForm::GetDocument() return (CEnrollDoc)m_pDocument; endif/ SectForm.cpp : implementation of the CSectionForm class#include "stdafx。h"include "Enroll.h"include "SectSet。h"include "CoursSet。h"#include ”EnrolDoc.h”incl

29、ude ”SectForm。h"ifdef _DEBUG#define new DEBUG_NEW#undef THIS_FILEstatic char THIS_FILE = _FILE_;endifIMPLEMENT_DYNCREATE(CSectionForm, CRecordView)BEGIN_MESSAGE_MAP(CSectionForm, CRecordView)/AFX_MSG_MAP(CSectionForm)ON_CBN_SELENDOK(IDC_COURSELIST, OnSelendokCourselist)ON_COMMAND(ID_RECORD_ADD,

30、 OnRecordAdd)ON_COMMAND(ID_RECORD_DELETE, OnRecordDelete)ON_COMMAND(ID_RECORD_REFRESH, OnRecordRefresh)/AFX_MSG_MAP/ Standard printing commandsON_COMMAND(ID_FILE_PRINT, CRecordView::OnFilePrint)ON_COMMAND(ID_FILE_PRINT_DIRECT, CRecordView:OnFilePrint)ON_COMMAND(ID_FILE_PRINT_PREVIEW, CRecordView::On

31、FilePrintPreview)END_MESSAGE_MAP()CSectionForm::CSectionForm(): CRecordView(CSectionForm:IDD)/AFX_DATA_INIT(CSectionForm)m_pSet = NULL;/AFX_DATA_INITm_bAddMode = FALSE;CSectionForm:CSectionForm()void CSectionForm::DoDataExchange(CDataExchange* pDX)CRecordView::DoDataExchange(pDX);/AFX_DATA_MAP(CSect

32、ionForm)DDX_Control(pDX, IDC_COURSELIST, m_ctlCourseList);DDX_FieldText(pDX, IDC_SECTION, m_pSet->m_SectionNo, m_pSet);DDX_Control(pDX, IDC_SECTION, m_ctlSection);DDX_FieldText(pDX, IDC_INSTRUCTOR, m_pSet->m_InstructorID, m_pSet);DDX_FieldText(pDX, IDC_ROOM, m_pSetm_RoomNo, m_pSet);DDX_FieldTe

33、xt(pDX, IDC_SCHEDULE, m_pSet->m_Schedule, m_pSet);DDX_FieldText(pDX, IDC_CAPACITY, m_pSet-m_Capacity, m_pSet);DDX_FieldCBString(pDX, IDC_COURSELIST, m_pSet->m_CourseID, m_pSet);/AFX_DATA_MAPBOOL CSectionForm::PreCreateWindow(CREATESTRUCT& cs)return CRecordView::PreCreateWindow(cs);void CSe

34、ctionForm:OnInitialUpdate()m_pSet = &GetDocument()->m_sectionSet;/ Fill the combo box with all of the coursesCEnrollDoc pDoc = GetDocument();pDoc->m_courseSet。m_strSort = "CourseID”;if (!pDoc->m_courseSet。Open()return;/ Filter, parameterize and sort the course recordsetm_pSet>m_

35、strFilter = ”CourseID = ?”;m_pSet->m_strCourseIDParam = pDoc-m_courseSet。m_CourseID;m_pSetm_strSort = ”SectionNo"m_pSet>m_pDatabase = pDocm_courseSet。m_pDatabase;CRecordView:OnInitialUpdate();m_ctlCourseList。ResetContent();if (pDocm_courseSet.IsOpen())while (!pDoc->m_courseSet。IsEOF())

36、m_ctlCourseList。AddString(pDoc->m_courseSet.m_CourseID);pDoc-m_courseSet。MoveNext();m_ctlCourseList.SetCurSel(0);BOOL CSectionForm:OnPreparePrinting(CPrintInfo pInfo)/ default preparationreturn DoPreparePrinting(pInfo);void CSectionForm:OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /pInfo*/)/ TODO: a

37、dd extra initialization before printingvoid CSectionForm::OnEndPrinting(CDC* /*pDC/, CPrintInfo /*pInfo/)/ TODO: add cleanup after printing#ifdef _DEBUGvoid CSectionForm:AssertValid() constCRecordView::AssertValid();void CSectionForm:Dump(CDumpContext dc) constCRecordView:Dump(dc);CEnrollDoc* CSecti

38、onForm:GetDocument() / nondebug version is inlineASSERT(m_pDocument-IsKindOf(RUNTIME_CLASS(CEnrollDoc)));return (CEnrollDoc)m_pDocument;endif /_DEBUGCRecordset CSectionForm:OnGetRecordset()return m_pSet;void CSectionForm::OnSelendokCourselist()if (!m_pSet-IsOpen()return;m_ctlCourseList.GetLBText(m_c

39、tlCourseList.GetCurSel(),m_pSetm_strCourseIDParam);if (!m_bAddMode)m_pSet-Requery();if (m_pSetIsEOF()m_pSet>SetFieldNull((m_pSet>m_CourseID), FALSE);m_pSet-m_CourseID = m_pSet-m_strCourseIDParam;UpdateData(FALSE);void CSectionForm:OnRecordAdd()/ If already in add mode, then complete previous new recordif (m_bAddMode)OnMove(ID_RECORD_FIRST);CString strCurrentCourse = m_pSet->m_CourseID;m_pSet-AddNew();m_pSetSetFieldNull(&(m_pSet-&g

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论