⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ex_odbcview.cpp

📁 Visual C++应用教程-源代码 本书在复习C++基础知识后
💻 CPP
字号:
// Ex_ODBCView.cpp : implementation of the CEx_ODBCView class
//

#include "stdafx.h"
#include "Ex_ODBC.h"

#include "Ex_ODBCSet.h"
#include "Ex_ODBCDoc.h"
#include "Ex_ODBCView.h"
#include "MainFrm.h"

#include "ScoreDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CEx_ODBCView

IMPLEMENT_DYNCREATE(CEx_ODBCView, CRecordView)

BEGIN_MESSAGE_MAP(CEx_ODBCView, CRecordView)
	//{{AFX_MSG_MAP(CEx_ODBCView)
	ON_BN_CLICKED(IDC_BUTTON_QUERY, OnButtonQuery)
	ON_BN_CLICKED(IDC_REC_ADD, OnRecAdd)
	ON_BN_CLICKED(IDC_REC_DEL, OnRecDel)
	ON_BN_CLICKED(IDC_REC_EDIT, OnRecEdit)
	//}}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()

/////////////////////////////////////////////////////////////////////////////
// CEx_ODBCView construction/destruction

CEx_ODBCView::CEx_ODBCView()
	: CRecordView(CEx_ODBCView::IDD)
{
	//{{AFX_DATA_INIT(CEx_ODBCView)
	m_pSet = NULL;
	m_strQuery = _T("");
	//}}AFX_DATA_INIT
	// TODO: add construction code here

}

CEx_ODBCView::~CEx_ODBCView()
{
}

void CEx_ODBCView::DoDataExchange(CDataExchange* pDX)
{
	CRecordView::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CEx_ODBCView)
	DDX_Text(pDX, IDC_EDIT_QUERY, m_strQuery);
	DDX_FieldText(pDX, IDC_STUNO, m_pSet->m_studentno, m_pSet);
	DDV_MaxChars(pDX, m_pSet->m_studentno, 8);
	DDX_FieldText(pDX, IDC_COURSENO, m_pSet->m_course, m_pSet);
	DDV_MaxChars(pDX, m_pSet->m_course, 7);
	DDX_FieldText(pDX, IDC_SCORE, m_pSet->m_score, m_pSet);
	DDV_MinMaxFloat(pDX, m_pSet->m_score, 0.f, 100.f);
	DDX_FieldText(pDX, IDC_CREDIT, m_pSet->m_credit, m_pSet);
	DDV_MinMaxFloat(pDX, m_pSet->m_credit, 1.f, 20.f);
	DDX_Control(pDX, IDC_MSFLEXGRID1, m_MSFGrid);
	//}}AFX_DATA_MAP
}

BOOL CEx_ODBCView::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs

	return CRecordView::PreCreateWindow(cs);
}

void CEx_ODBCView::OnInitialUpdate()
{
	m_pSet = &GetDocument()->m_ex_ODBCSet;
	CRecordView::OnInitialUpdate();
	GetParentFrame()->RecalcLayout();
	ResizeParentToFit();
	while (!m_pSet->IsEOF())
	{
		m_pSet->MoveNext();
		m_pSet->GetRecordCount();
	}
	m_pSet->MoveFirst();

	m_MSFGrid.SetCols(m_pSet->m_nFields+1 );		// 根据字段个数,设置单元格的最大列数
	m_MSFGrid.SetRows(m_pSet->GetRecordCount()+1);	// 根据记录数,设置单元格的最大行数
	m_MSFGrid.SetColWidth(-1,1440);					
	// 将所有的单元格都设为相同的列宽。-1表示所有的列,列宽单位为一个点的
	// 1/20(一个点是1/72英寸),也就是说,1440刚好为1英寸。

	// 定义单元格的表头
	m_MSFGrid.SetRow(0);	m_MSFGrid.SetCol(0);	// 定位到(0,0)单元格
	m_MSFGrid.SetText("记录号");	// 设置其显示内容
	m_MSFGrid.SetCellAlignment(4);	// 设置单元格对齐方式,4表示水平和垂直居中
	CODBCFieldInfo field;
	for (UINT i=0; i<m_pSet->m_nFields; i++)
	{
		m_MSFGrid.SetRow(0);	m_MSFGrid.SetCol(i+1);	
		m_pSet->GetODBCFieldInfo(i,field);			// 获取指定字段信息
		m_MSFGrid.SetText(field.m_strName);	
		m_MSFGrid.SetCellAlignment(4);	
	}

	int iRow=1;
	while (!m_pSet->IsEOF())					//将表的记录内容显示在单元格中
	{
		CString str;
		str.Format("记录%d",iRow);
		m_MSFGrid.SetRow(iRow);	m_MSFGrid.SetCol(0);
		m_MSFGrid.SetText(str);
		m_MSFGrid.SetCellAlignment(4);	
		for (UINT i=0; i<m_pSet->m_nFields; i++)
		{
			m_MSFGrid.SetRow(iRow);	m_MSFGrid.SetCol(i+1);	
			m_pSet->GetFieldValue(i, str);			// 获取指定字段值,并自动转换成字符串
			m_MSFGrid.SetText(str);	
			m_MSFGrid.SetCellAlignment(4);	
		}
		iRow++;
		m_pSet->MoveNext();
	}
	m_MSFGrid.SetRow(1);		m_MSFGrid.SetCol(1);
	m_pSet->MoveFirst();
}

/////////////////////////////////////////////////////////////////////////////
// CEx_ODBCView printing

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

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

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

/////////////////////////////////////////////////////////////////////////////
// CEx_ODBCView diagnostics

#ifdef _DEBUG
void CEx_ODBCView::AssertValid() const
{
	CRecordView::AssertValid();
}

void CEx_ODBCView::Dump(CDumpContext& dc) const
{
	CRecordView::Dump(dc);
}

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

/////////////////////////////////////////////////////////////////////////////
// CEx_ODBCView database support
CRecordset* CEx_ODBCView::OnGetRecordset()
{
	return m_pSet;
}


/////////////////////////////////////////////////////////////////////////////
// CEx_ODBCView message handlers

void CEx_ODBCView::OnButtonQuery() 
{
	// TODO: Add your control notification handler code here
	UpdateData();
	m_strQuery.TrimLeft();
	if (m_strQuery.IsEmpty())
	{
		MessageBox("要查询的学号不能为空!");
		return;
	}
	if (m_pSet->IsOpen())
		m_pSet->Close();		// 如果记录集打开,则先关闭
	m_pSet->m_strFilter.Format("studentno='%s'",m_strQuery);	// studentno是score表的字段名
	m_pSet->m_strSort = "course";	// course是score表的字段名
	m_pSet->Open();
	if (!m_pSet->IsEOF())		// 如果打开记录集有记录
		UpdateData(FALSE);		// 自动更新表单中控件显示的内容
	else
		MessageBox("没有查到你要找的学号记录!");
}

BOOL CEx_ODBCView::OnCommand(WPARAM wParam, LPARAM lParam) 
{
	// TODO: Add your specialized code here and/or call the base class
	CString str;
	CMainFrame* pFrame = (CMainFrame*)AfxGetApp()->m_pMainWnd;
 									// 获得主框架窗口的指针
	CStatusBar* pStatus = &pFrame->m_wndStatusBar;
									// 获得主框架窗口中的状态栏指针
	if (pStatus)
	{
			CRecordsetStatus rStatus; 
			m_pSet->GetStatus(rStatus);		// 获得当前记录信息
			str.Format("当前记录:%d/总记录:%d",1+rStatus.m_lCurrentRecord,
			m_pSet->GetRecordCount());
			pStatus->SetPaneText(1,str);		// 更新第二个窗格的文本
	}
	
	return CRecordView::OnCommand(wParam, lParam);
}

void CEx_ODBCView::OnRecAdd() 
{
	// TODO: Add your control notification handler code here
	CScoreDlg dlg;
	if (dlg.DoModal()==IDOK){
		m_pSet->AddNew();
		m_pSet->m_course		= dlg.m_strCourseNO;
		m_pSet->m_studentno		= dlg.m_strStudentNO;
		m_pSet->m_score			= dlg.m_fScore;
		m_pSet->m_credit		= dlg.m_fCredit;
		m_pSet->Update();
		m_pSet->Requery();
	}
}

void CEx_ODBCView::OnRecEdit() 
{
	// TODO: Add your control notification handler code here
	CScoreDlg dlg;
	dlg.m_strCourseNO	= m_pSet->m_course;	
	dlg.m_strStudentNO	= m_pSet->m_studentno;	
	dlg.m_fScore		= m_pSet->m_score;		
	dlg.m_fCredit		= m_pSet->m_credit;	
	if (dlg.DoModal()==IDOK){
		m_pSet->Edit();
		m_pSet->m_course		= dlg.m_strCourseNO;
		m_pSet->m_studentno		= dlg.m_strStudentNO;
		m_pSet->m_score			= dlg.m_fScore;
		m_pSet->m_credit		= dlg.m_fCredit;
		m_pSet->Update();
		UpdateData(FALSE);
	}
}

void CEx_ODBCView::OnRecDel() 
{
	// TODO: Add your control notification handler code here
	CRecordsetStatus status;
	m_pSet->GetStatus(status);
	m_pSet->Delete();
	if (status.m_lCurrentRecord==0)
		m_pSet->MoveNext();
	else
		m_pSet->MoveFirst();
	UpdateData(FALSE);	
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -