📄 studentview.cpp
字号:
// StudentView.cpp : implementation of the CStudentView class
//
#include "stdafx.h"
#include "Student.h"
#include "StudentDoc.h"
#include "StudentView.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CStudentView
IMPLEMENT_DYNCREATE(CStudentView, CListView)
BEGIN_MESSAGE_MAP(CStudentView, CListView)
//{{AFX_MSG_MAP(CStudentView)
ON_UPDATE_COMMAND_UI(ID_TABLE_COURSE, OnUpdateTableCourse)
ON_UPDATE_COMMAND_UI(ID_TABLE_SCORE, OnUpdateTableScore)
ON_UPDATE_COMMAND_UI(ID_TABLE_STUDENT, OnUpdateTableStudent)
ON_COMMAND(ID_OP_ADD, OnOpAdd)
ON_COMMAND(ID_OP_DEL, OnOpDel)
ON_COMMAND(ID_OP_EDIT, OnOpEdit)
ON_COMMAND(ID_OP_SEEK, OnOpSeek)
ON_COMMAND(ID_OP_STAT, OnOpStat)
ON_COMMAND(ID_TABLE_COURSE, OnTableCourse)
ON_COMMAND(ID_TABLE_SCORE, OnTableScore)
ON_COMMAND(ID_TABLE_STUDENT, OnTableStudent)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CStudentView construction/destruction
CStudentView::CStudentView()
{
// TODO: add construction code here
}
CStudentView::~CStudentView()
{
}
BOOL CStudentView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
cs.style |= LVS_REPORT | LVS_SHOWSELALWAYS ;
return CListView::PreCreateWindow(cs);
}
/////////////////////////////////////////////////////////////////////////////
// CStudentView drawing
void CStudentView::OnDraw(CDC* pDC)
{
CStudentDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
}
void CStudentView::OnInitialUpdate()
{
CListView::OnInitialUpdate();
CListCtrl& m_ListCtrl = GetListCtrl();
m_ListCtrl.SetExtendedStyle(LVS_EX_FULLROWSELECT|LVS_EX_GRIDLINES);
m_pConnection.CreateInstance(__uuidof(Connection)); // 初始化Connection指针
m_pConnection->ConnectionString
="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=..\\..\\student.mdb;";
// 此句代码在一行书写
m_pConnection->ConnectionTimeout = 30; // 允许连接超时时间,单位为秒
HRESULT hr = m_pConnection->Open("","","",0);
if (hr != S_OK)
MessageBox("无法连接指定的数据库!");
m_nTable = 1;
DispAllRec("SELECT * FROM student ORDER BY XH");
// TODO: You may populate your ListView with items by directly accessing
// its list control through a call to GetListCtrl().
}
/////////////////////////////////////////////////////////////////////////////
// CStudentView diagnostics
#ifdef _DEBUG
void CStudentView::AssertValid() const
{
CListView::AssertValid();
}
void CStudentView::Dump(CDumpContext& dc) const
{
CListView::Dump(dc);
}
CStudentDoc* CStudentView::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CStudentDoc)));
return (CStudentDoc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CStudentView message handlers
void CStudentView::OnUpdateTableCourse(CCmdUI* pCmdUI)
{
// TODO: Add your command update UI handler code here
pCmdUI->SetRadio( m_nTable == 2 );
}
void CStudentView::OnUpdateTableScore(CCmdUI* pCmdUI)
{
// TODO: Add your command update UI handler code here
pCmdUI->SetRadio( m_nTable == 3 );
}
void CStudentView::OnUpdateTableStudent(CCmdUI* pCmdUI)
{
// TODO: Add your command update UI handler code here
pCmdUI->SetRadio( m_nTable == 1 );
}
void CStudentView::OnOpAdd()
{
// TODO: Add your command handler code here
}
void CStudentView::OnOpDel()
{
// TODO: Add your command handler code here
}
void CStudentView::OnOpEdit()
{
// TODO: Add your command handler code here
}
void CStudentView::OnOpSeek()
{
// TODO: Add your command handler code here
}
void CStudentView::OnOpStat()
{
// TODO: Add your command handler code here
}
void CStudentView::OnTableCourse()
{
// TODO: Add your command handler code here
m_nTable = 2;
DispAllRec("SELECT * FROM course ORDER BY KCH");
}
void CStudentView::OnTableScore()
{
// TODO: Add your command handler code here
m_nTable = 3;
DispAllRec("SELECT * FROM score ORDER BY XH");
}
void CStudentView::OnTableStudent()
{
// TODO: Add your command handler code here
m_nTable = 1;
DispAllRec("SELECT * FROM student ORDER BY XH");
}
void CStudentView::DispAllRec(CString strSQL)
{
CListCtrl& m_ListCtrl = GetListCtrl();
m_ListCtrl.DeleteAllItems();
int nColumnCount = m_ListCtrl.GetHeaderCtrl()->GetItemCount();
for (int i=0; i<nColumnCount; i++)
m_ListCtrl.DeleteColumn(0);
_CommandPtr pCmd;
pCmd.CreateInstance(__uuidof(Command)); // 初始化Command指针
pCmd->ActiveConnection = m_pConnection; // 指向已有的连接
pCmd->CommandText = _bstr_t(strSQL);
_RecordsetPtr pSet;
pSet.CreateInstance(__uuidof(Recordset)); // 初始化Recordset指针
pSet = pCmd->Execute(NULL, NULL, adCmdText );
// 建立列表控件的列表头
FieldsPtr flds = pSet->GetFields(); // 获取当前表的字段指针
_variant_t Index;
Index.vt = VT_I2;
m_ListCtrl.InsertColumn(0, "序号", LVCFMT_LEFT, 40 );
for (i = 0; i < (int)flds->GetCount(); i++) {
Index.iVal = i;
int nWidth = flds->GetItem(Index)->GetDefinedSize()*9;
if ( nWidth < 40 ) nWidth = 40;
if ( nWidth > 200 ) nWidth = 200;
m_ListCtrl.InsertColumn(i+1, (LPSTR)flds->GetItem(Index)->GetName(),
LVCFMT_LEFT, nWidth);
}
// 显示记录
_bstr_t str, value;
int nItem = 0;
CString strItem;
while(!pSet->adoEOF){
strItem.Format("%d", nItem+1);
m_ListCtrl.InsertItem(nItem, strItem );
for (i = 0; i < (int)flds->GetCount(); i++) {
Index.iVal = i;
str = flds->GetItem(Index)->GetName();
try{
value = pSet->GetCollect(str);
m_ListCtrl.SetItemText( nItem, i+1, (LPCSTR)value );
}catch(...)
{}
}
pSet->MoveNext();
nItem++;
}
pSet->Close();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -