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

📄 daolistview.cpp

📁 Visual C++ 实践与提高--数据库篇的源代码。很好的东西。欢迎下载。
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// DaoListView.cpp : implementation file
//

#include "stdafx.h"
#include "DAOQry.h"
#include "DaoListView.h"
#include "DAOQryDoc.h"
#include "ctrlext.h"
#include "crack.h"
#include "DlgParams.h"

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

/////////////////////////////////////////////////////////////////////////////
// CDaoListView

IMPLEMENT_DYNCREATE(CDaoListView, CListView)

BEGIN_MESSAGE_MAP(CDaoListView, CListView)
	//{{AFX_MSG_MAP(CDaoListView)
	ON_WM_CREATE()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CDaoListView construction/destruction

CDaoListView::CDaoListView()
{
}

CDaoListView::~CDaoListView()
{
}

/////////////////////////////////////////////////////////////////////////////
// CDaoListView drawing

void CDaoListView::OnDraw(CDC* pDC)
{
	CDAOQryDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);

	// TODO: add draw code for native data here
}

/////////////////////////////////////////////////////////////////////////////
// CDaoListView diagnostics

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

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

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

/////////////////////////////////////////////////////////////////////////////
// CDaoListView message handlers

int CDaoListView::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
	lpCreateStruct->style |= LVS_REPORT;
	if (CListView::OnCreate(lpCreateStruct) == -1)
		return -1;

	// Give the document a pointer to this view
	GetDocument()->m_pListView = this;

	return 0;
}

void CDaoListView::EraseList()
{
	CListCtrlEx& ctlList = (CListCtrlEx&) GetListCtrl();
	ctlList.DeleteAllItems();
	while(ctlList.DeleteColumn(0));
	UpdateWindow();
}

void CDaoListView::ShowDatabase()
{
	ASSERT(m_pDB);
	ASSERT(m_pDB->IsOpen());

	CDaoDatabaseInfo dbInfo;

	m_bVertical = TRUE;
	DisplayColumnHeadings(IDS_COL_DATABASE);

	try
	{
		m_pDB->m_pWorkspace->GetDatabaseInfo(m_pDB->GetName(),dbInfo,AFX_DAO_ALL_INFO);
		ShowDatabaseInfo(0,dbInfo);
	}
	catch(CDaoException* e)
	{
		e->Delete();
	}
	AdjustColumnWidths();
}

void CDaoListView::ShowTableData(LPCTSTR strTableName)
{
	//确保操作的数据库有效
	ASSERT(m_pDB);
	ASSERT(m_pDB->IsOpen());

	CListCtrlEx& ctlList = (CListCtrlEx&) GetListCtrl();

	CDaoFieldInfo fieldInfo;
	int nFields;
	//刷新列表视
	EraseList();

	//构造一个表定义对象
	CDaoTableDef td(m_pDB);
	try
	{
		//初始化表定义对象
		td.Open(strTableName);
		//获得表定义中的字段数目
		nFields = td.GetFieldCount();
		for (int j=0; j < nFields; j++)
		{
			//获得指定字段的信息
			td.GetFieldInfo(j,fieldInfo);
			ctlList.AddColumn(fieldInfo.m_strName,j);
		}
	}
	catch (CDaoException* e)
	{
		DisplayDaoException(e);
		e->Delete();
	}
	//释放表定义对象
	td.Close();

	//得到记录行信息并插入列表
	CDaoRecordset rs(m_pDB);
	int nItem = 0;
	int nLoaded = 0;
	BOOL MAXRECORDS = ((CDAOQryApp *)AfxGetApp())->m_nMaxRecords;
	try
	{
		//执行SQL查询
		CString strSelect(_T("Select * From ["));
		strSelect += strTableName;
		strSelect += _T("]");
		rs.Open(dbOpenDynaset,strSelect);

		while (!rs.IsEOF()) {
			if (nItem < MAXRECORDS)\
			{
				nLoaded++;
				//注意下面第五句CCrack的类型转换
				COleVariant var;
				for (int i=0; i < nFields; i++)
				{
					var = rs.GetFieldValue(i);
					ctlList.AddItem(nItem,i,CCrack::strVARIANT(var));
				}
			}
			nItem++;
			//移动记录游标
			rs.MoveNext();
		}
	}
	catch (CDaoException* e)
	{
		DisplayDaoException(e);
		e->Delete();
		return;
	}
	CString strRecCount;
	strRecCount.Format(_T("Loaded %d of %d total records"),nLoaded,nItem);
	UpdateWindow();
	if (nItem>=MAXRECORDS)
		MessageBox(strRecCount);
	((CFrameWnd *) AfxGetMainWnd())->SetMessageText(strRecCount);
	rs.Close();
}

void CDaoListView::ShowTableSchema(LPCTSTR strTableDefName)
{
	//确保数据库可操作的(打开状态)
	ASSERT(m_pDB);
	ASSERT(m_pDB->IsOpen());

	//声明表定义对象
	CDaoTableDefInfo tabInfo;

	m_bVertical = strTableDefName != NULL;
	//显示表头信息
	DisplayColumnHeadings(IDS_COL_TABLE);

	int nItem = 0;
	int nTableDefCount = m_pDB->GetTableDefCount();
	BOOL bShowSystemObjects = ((CDAOQryApp *)AfxGetApp())->m_bShowSystemObjects;
	for (int j=0; j < nTableDefCount; j++)
	{
		try
		{
			//获得表信息
			m_pDB->GetTableDefInfo(j,tabInfo,AFX_DAO_ALL_INFO);
			if (!bShowSystemObjects)
				if (tabInfo.m_lAttributes & dbSystemObject)
					continue;
			//显示表定义信息
			if (strTableDefName == NULL || tabInfo.m_strName == strTableDefName)
				ShowTableDefInfo(nItem++,tabInfo);
		}
		catch(CDaoException* e)
		{
			e->Delete();
		}
	}
	//调整列宽
	AdjustColumnWidths();
}

void CDaoListView::ShowRelations(LPCTSTR strRelationName)
{
	ASSERT(m_pDB);
	ASSERT(m_pDB->IsOpen());

	CDaoRelationInfo Info;

	m_bVertical = strRelationName != NULL;
	DisplayColumnHeadings(IDS_COL_RELATION);

	int nItem = 0;
	int nTableDefCount = m_pDB->GetTableDefCount();
	BOOL bShowSystemObjects = ((CDAOQryApp *)AfxGetApp())->m_bShowSystemObjects;
	for (int j=0; j < nTableDefCount; j++)
	{
		try
		{
			m_pDB->GetRelationInfo(j,Info,AFX_DAO_ALL_INFO);
			if (!bShowSystemObjects)
				if (Info.m_lAttributes & dbSystemObject)
					continue;
			if (strRelationName == NULL || Info.m_strName == strRelationName)
				ShowRelationInfo(nItem++,Info);
		}
		catch(CDaoException* e)
		{
			e->Delete();
		}
	}
	AdjustColumnWidths();
}

void CDaoListView::ShowIndexes(LPCTSTR strTableName,LPCTSTR strIndexName)
{
	ASSERT(m_pDB);
	ASSERT(m_pDB->IsOpen());

	CDaoTableDef td(m_pDB);
	CDaoIndexInfo Info;

	m_bVertical = strIndexName != NULL;
	DisplayColumnHeadings(IDS_COL_INDEX);

	try
	{
		td.Open(strTableName);
		int nItem = 0;
		int nIndexCount = td.GetIndexCount();
		for (int j=0; j < nIndexCount; j++)
		{
			try
			{
				td.GetIndexInfo(j,Info,AFX_DAO_ALL_INFO);
				if (strIndexName == NULL || Info.m_strName == strIndexName)
					ShowIndexInfo(nItem++,Info);
			}
			catch(CDaoException* e)
			{
				e->Delete();
			}
		}
		AdjustColumnWidths();
	}
	catch(CDaoException* e)
	{
		e->Delete();
	}
	td.Close();
	AdjustColumnWidths();
}

void CDaoListView::ShowQuerySchema(LPCTSTR strQueryDefName)
{
	ASSERT(m_pDB);
	ASSERT(m_pDB->IsOpen());

	//声明查询定义对象
	CDaoQueryDefInfo qdInfo;

	m_bVertical = strQueryDefName != NULL;
	//表头信息
	DisplayColumnHeadings(IDS_COL_QUERYDEF);

	int nItem = 0;
	//获得库中的查询定义计数
	int nQueryDefCount = m_pDB->GetQueryDefCount();
	BOOL bShowSystemObjects = ((CDAOQryApp *)AfxGetApp())->m_bShowSystemObjects;

⌨️ 快捷键说明

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