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

📄 ex_adoview.cpp

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

#include "stdafx.h"
#include "Ex_ADO.h"

#include "Ex_ADODoc.h"
#include "Ex_ADOView.h"

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

/////////////////////////////////////////////////////////////////////////////
// CEx_ADOView

IMPLEMENT_DYNCREATE(CEx_ADOView, CListView)

BEGIN_MESSAGE_MAP(CEx_ADOView, CListView)
	//{{AFX_MSG_MAP(CEx_ADOView)
	ON_WM_DESTROY()
	ON_COMMAND(ID_VIEW_COURSE, OnViewCourse)
	//}}AFX_MSG_MAP
	// Standard printing commands
	ON_COMMAND(ID_FILE_PRINT, CListView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_DIRECT, CListView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_PREVIEW, CListView::OnFilePrintPreview)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CEx_ADOView construction/destruction

CEx_ADOView::CEx_ADOView()
{
	// TODO: add construction code here

}

CEx_ADOView::~CEx_ADOView()
{
}

BOOL CEx_ADOView::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs
	cs.style |= LVS_REPORT;		// 报表风格
	return CListView::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// CEx_ADOView drawing

void CEx_ADOView::OnDraw(CDC* pDC)
{
	CEx_ADODoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	// TODO: add draw code for native data here
}

void CEx_ADOView::OnInitialUpdate()
{
	CListView::OnInitialUpdate();


	// TODO: You may populate your ListView with items by directly accessing
	//  its list control through a call to GetListCtrl().
	m_pConnection.CreateInstance(__uuidof(Connection)); 	// 初始化Connection指针
	m_pRecordset.CreateInstance(__uuidof(Recordset));		// 初始化Recordset指针
	m_pCommand.CreateInstance(__uuidof(Command));	// 初始化Recordset指针
	// 连接数据源为"Database Example For VC++"
	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("无法连接指定的数据库!");
	// 获取数据表名和字段名
	_RecordsetPtr  pRstSchema  = NULL;				// 定义一个记录集指针
	pRstSchema = m_pConnection->OpenSchema(adSchemaColumns);	// 获取表信息
	// 将表信息显示在列表视图控件中
	CListCtrl& m_ListCtrl = GetListCtrl();
	CString strHeader[3] = {"序号","TABLE_NAME","COLUMN_NAME"};
	for (int i=0; i<3; i++)
		m_ListCtrl.InsertColumn( i, strHeader[i], LVCFMT_LEFT, 120);
	int nItem = 0;
	CString str;
	_bstr_t value;
	while(!(pRstSchema->adoEOF)) {
		str.Format("%d", nItem+1 );
		m_ListCtrl.InsertItem( nItem, str );
		for (int i=1; i<3; i++) {
			value = pRstSchema->Fields->GetItem((_bstr_t)(LPCSTR)strHeader[i])->Value;
			m_ListCtrl.SetItemText( nItem, i, value );
		}
		pRstSchema->MoveNext();
		nItem++;
	}
	pRstSchema->Close();
}

/////////////////////////////////////////////////////////////////////////////
// CEx_ADOView printing

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

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

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

/////////////////////////////////////////////////////////////////////////////
// CEx_ADOView diagnostics

#ifdef _DEBUG
void CEx_ADOView::AssertValid() const
{
	CListView::AssertValid();
}

void CEx_ADOView::Dump(CDumpContext& dc) const
{
	CListView::Dump(dc);
}

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

/////////////////////////////////////////////////////////////////////////////
// CEx_ADOView message handlers

void CEx_ADOView::OnDestroy() 
{
	CListView::OnDestroy();
	
	// TODO: Add your message handler code here
	if (m_pConnection)
		m_pConnection->Close();// 关闭连接	

}

void CEx_ADOView::OnViewCourse() 
{
	// TODO: Add your command handler code here
	CListCtrl& m_ListCtrl = GetListCtrl();
	// 删除列表中所有行和列表头
	m_ListCtrl.DeleteAllItems();
	int nColumnCount = m_ListCtrl.GetHeaderCtrl()->GetItemCount();
	for (int i=0; i<nColumnCount; i++)
		m_ListCtrl.DeleteColumn(0);
	m_pRecordset->Open( "Course",		// 指定要打开的表
		m_pConnection.GetInterfacePtr(),	// 获取当前数据库连接的接口指针
		adOpenDynamic,				// 动态游标类型,可以使用Move等操作
		adLockOptimistic,		
		adCmdTable);
	// 建立列表控件的列表头
	FieldsPtr flds = m_pRecordset->GetFields();		// 获取当前表的字段指针
	_variant_t Index;
	Index.vt = VT_I2;
	m_ListCtrl.InsertColumn(0, "序号", LVCFMT_LEFT, 60 );
	for (i = 0; i < (int)flds->GetCount(); i++)   {
		Index.iVal=i;
		m_ListCtrl.InsertColumn(i+1, (LPSTR)flds->GetItem(Index)->GetName(), 
			LVCFMT_LEFT, 140 );
 	}
	// 显示记录
	_bstr_t str, value;
	int nItem = 0;
	CString strItem;
	while(!m_pRecordset->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();
			value = m_pRecordset->GetCollect(str);
			m_ListCtrl.SetItemText( nItem, i+1, (LPCSTR)value );
		}
		m_pRecordset->MoveNext();
		nItem++;
	}
	m_pRecordset->Close();	
	
}

⌨️ 快捷键说明

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