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

📄 virtuallistdoc.cpp

📁 DAO数据库编程
💻 CPP
字号:
// VirtualListDoc.cpp : implementation of the CVirtualListDoc class
//

#include "stdafx.h"
#include "VirtualList.h"

#include <afxcview.h>

#include "VirtualListDoc.h"
#include "PickTableDlg.h"

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

/////////////////////////////////////////////////////////////////////////////
// CVirtualListDoc

IMPLEMENT_DYNCREATE(CVirtualListDoc, CDocument)

BEGIN_MESSAGE_MAP(CVirtualListDoc, CDocument)
	//{{AFX_MSG_MAP(CVirtualListDoc)
		// NOTE - the ClassWizard will add and remove mapping macros here.
		//    DO NOT EDIT what you see in these blocks of generated code!
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CVirtualListDoc construction/destruction

CVirtualListDoc::CVirtualListDoc()
{
	// TODO: add one-time construction code here
	m_pDataBase = NULL;
	m_pRecordSet = NULL;
}

CVirtualListDoc::~CVirtualListDoc()
{
}

BOOL CVirtualListDoc::OnNewDocument()
{
	if (!CDocument::OnNewDocument())
		return FALSE;

	// TODO: add reinitialization code here
	// (SDI documents will reuse this document)

	return TRUE;
}



/////////////////////////////////////////////////////////////////////////////
// CVirtualListDoc serialization

/*void CVirtualListDoc::Serialize(CArchive& ar)
{
	if (ar.IsStoring())
	{
		// TODO: add storing code here
	}
	else
	{
		// TODO: add loading code here
	}
}*/

/////////////////////////////////////////////////////////////////////////////
// CVirtualListDoc diagnostics

#ifdef _DEBUG
void CVirtualListDoc::AssertValid() const
{
	CDocument::AssertValid();
}

void CVirtualListDoc::Dump(CDumpContext& dc) const
{
	CDocument::Dump(dc);
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CVirtualListDoc commands

void CVirtualListDoc::OnCloseDocument() 
{
	// TODO: Add your specialized code here and/or call the base class
	POSITION pos = GetFirstViewPosition();
	while(pos != NULL)
	{
		CListView* pCurrentView = (CListView*)GetNextView(pos);
		CImageList* pImageList = pCurrentView->GetListCtrl().SetImageList(NULL, LVSIL_SMALL);
		delete pImageList;
	}
	
	if(m_pRecordSet != NULL)
	{
		ASSERT_VALID(m_pRecordSet);
		if(m_pRecordSet->IsOpen())
			m_pRecordSet->Close();
		delete m_pRecordSet;
		m_pRecordSet = NULL;
	}

	if(m_pDataBase != NULL)
	{
		ASSERT_VALID(m_pDataBase);
		if(m_pDataBase->IsOpen())
			m_pDataBase->Close();
		delete m_pDataBase;
		m_pDataBase = NULL;
	}
	CDocument::OnCloseDocument();	
}

BOOL CVirtualListDoc::OnOpenDocument(LPCTSTR lpszPathName) 
{
/*	if (!CDocument::OnOpenDocument(lpszPathName))
		return FALSE;
	
	// TODO: Add your specialized creation code here
	
	return TRUE;
*/
	if (!CDocument::OnOpenDocument(lpszPathName))
		return FALSE;
	
	m_pDataBase = new CDaoDatabase;
	try
	{
		m_pDataBase->Open(lpszPathName);
	}
	catch(CDaoException* e)
	{
		AfxMessageBox(e->m_pErrorInfo->m_strDescription, MB_ICONEXCLAMATION );
		delete m_pDataBase;
		e->Delete();
		return FALSE;
	}
	CPickTableDlg  pickDlg(m_pDataBase);
	if(pickDlg.DoModal() == IDCANCEL)
		return FALSE;
	CString TableName;
	pickDlg.GetTableName(TableName);
	m_pRecordSet = new CDaoRecordset(m_pDataBase);
	//创建SQL语句
	CString strSQL = "SELECT * FROM " + TableName;	
	try
	{
		//用SQL语句打开记录集
		m_pRecordSet->Open(dbOpenDynaset, strSQL);
	}
	catch(CDaoException* e)
	{
		AfxMessageBox(e->m_pErrorInfo->m_strDescription, 
				MB_ICONEXCLAMATION );
		delete m_pRecordSet;
		m_pDataBase->Close();
		delete m_pDataBase;
		e->Delete();
		return FALSE;
	}
	//you have to access the records in the dynaset to get GCDaoRecordSet::SetRecordCount() to work	return TRUE;
	m_pRecordSet->MoveLast();
	return TRUE;
}

void CVirtualListDoc::DeleteContents() 
{
	// TODO: Add your specialized code here and/or call the base class
	POSITION pos = GetFirstViewPosition();
	while(pos != NULL)
	{
		CListView* pCurrentView = (CListView*)GetNextView(pos);
		int NumCols = 0;
		LV_COLUMN lvColumn;
		memset(&lvColumn, 0, sizeof(LV_COLUMN));
		lvColumn.mask = LVCF_FMT;
		while(pCurrentView->GetListCtrl().GetColumn(NumCols, &lvColumn))
			NumCols++;
		pCurrentView->GetListCtrl().SendMessage(LVM_SETITEMCOUNT, (WPARAM)0, (LPARAM)LVSICF_NOINVALIDATEALL);
		while(NumCols > 0)
		{
			pCurrentView->GetListCtrl().DeleteColumn(NumCols - 1);
			NumCols--;
		}
		CImageList* pImageList = pCurrentView->GetListCtrl().SetImageList(NULL, LVSIL_SMALL);
		delete pImageList;
	}
	
	if(m_pRecordSet != NULL)
	{
		ASSERT_VALID(m_pRecordSet);
		if(m_pRecordSet->IsOpen())
			m_pRecordSet->Close();
		delete m_pRecordSet;
		m_pRecordSet = NULL;
	}

	if(m_pDataBase != NULL)
	{
		ASSERT_VALID(m_pDataBase);
		if(m_pDataBase->IsOpen())
			m_pDataBase->Close();
		delete m_pDataBase;
		m_pDataBase = NULL;
	}
	CDocument::DeleteContents();	
}

⌨️ 快捷键说明

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