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

📄 readme.wzd

📁 《Visual C++ MFC编程实例》配套代码,如果大家正在学习此教程
💻 WZD
字号:
/////////////////////////////////////////////////////////////////
// Modify Stdafx.h
/////////////////////////////////////////////////////////////////


// 1) add these includes if they don't already exist

#ifndef _AFX_NO_DB_SUPPORT
#include <afxdb.h>			// MFC ODBC database classes
#endif // _AFX_NO_DB_SUPPORT

#ifndef _AFX_NO_DAO_SUPPORT
#include <afxdao.h>			// MFC DAO database classes
#endif // _AFX_NO_DAO_SUPPORT



/////////////////////////////////////////////////////////////////
// Modify Document Class
/////////////////////////////////////////////////////////////////

// 1) embed database class in document class or any other class
	CDaoDocument m_WzdDocument;

// 2) open database in CDocument's OnNewFile() or OnOpenFile()
	try
	{
		m_WzdDatabase.Open(
			"C:\\jeswanke\\examples\\Sampdata.mdb", // Data source name, NULL if defined in DSN= below
			FALSE,								// exclusive access to database
			FALSE);								// read only access
	}
	catch (CDaoException *e)
	{
 		AfxMessageBox(e->m_pErrorInfo->m_strDescription);
		e->Delete();
		return FALSE;
	}

	

// 3) close database in CDocument's DeleteContents()
	if (m_WzdDatabase.IsOpen())
		m_WzdDatabase.Close();



/////////////////////////////////////////////////////////////////
// Modify View Class
/////////////////////////////////////////////////////////////////

// 1) create record set object
	CWzdRecordset wzdSet(GetDocument()->GetDatabase());

// 2) open all records in table
	wzdSet.Open();

// 3) scroll through records
	while (! wzdSet.IsEOF())
	{
		// values can be accessed from record set member variables:
		//		wzdSet.m_CustomerID  
		//		wzdSet.m_CompanyName 
		//		wzdSet.m_ContactName 
		//			etc.

		wzdSet.MoveNext();
	}

// 4) find all records with a particular column value
	if (wzdSet.FindFirst("[Country]='UK'"))
	{
		while (wzdSet.FindNext("[Country]='UK'"))
		{
		}
	}

// 5) if we can't update or append records, leave now
	if (!wzdSet.CanUpdate() || !wzdSet.CanAppend())
		return;

// 6) add a record
	try
	{
		// add new record
		wzdSet.AddNew();

		// initialize each field
		wzdSet.m_CustomerID="ABCDX";
		wzdSet.m_CompanyName="ABC Inc.";
		//etc.

		// update database
		wzdSet.Update();
	}
	catch (CDaoException *e)
	{
		// AddNew failed
 		AfxMessageBox(e->m_pErrorInfo->m_strDescription);
		e->Delete();
	}

// 7) edit a record
	wzdSet.MoveFirst();
	try
	{
		wzdSet.Edit();

		// update affected fields
		wzdSet.m_CompanyName="ABCEF Inc.";
		wzdSet.m_ContactName="Frank";
		//etc.

		// update database
		wzdSet.Update();
	}
	catch (CDaoException *e)
	{
		// Edit failed
 		AfxMessageBox(e->m_pErrorInfo->m_strDescription);
		e->Delete();
	}


// 8) delete a record
	try
	{
		// delete record to which we're currently opened
		wzdSet.Delete();
	}
	catch (CDaoException *e)
	{
		// Delete failed
 		AfxMessageBox(e->m_pErrorInfo->m_strDescription);
		e->Delete();
	}

// 9) close record set
	wzdSet.Close();

///////////////////////////////////
// RECORD FILTERING
///////////////////////////////////

// 10) open record set with a simple "WHERE" SQL filter
	wzdSet.m_strFilter = "[Country]='UK'";
// sort record set with a simple "ORDER BY" SQL specification
	wzdSet.m_strSort = "[ContactName]";
	wzdSet.Open();
	if(wzdSet.IsEOF())
	{
		AfxMessageBox("Cannot find records.");
	}

	// scroll through records as above...
	while (! wzdSet.IsEOF())
	{
		wzdSet.MoveNext();
	}
	wzdSet.Close();


// 11) open a record set with a complete SQL SELECT statement
	wzdSet.m_strFilter = ""; // appended to the following!
	wzdSet.m_strSort = "";
	wzdSet.Open(AFX_DB_USE_DEFAULT_TYPE,
	 	"SELECT [CustomerID], [CompanyName], [ContactName], \
[ContactTitle], [Address], [City], [Region], [PostalCode], [Country], [Phone], [Fax] \
FROM [Customers] WHERE [Country] = 'Mexico'");

	if (wzdSet.IsEOF())
	{
		AfxMessageBox("Cannot find records.");
	}

	// scroll through records as above...
	while (! wzdSet.IsEOF())
	{
		wzdSet.MoveNext();
	}
	wzdSet.Close();

////////////////////////////////////
// MULTIPLE TABLE TRANSACTIONS
////////////////////////////////////

// NOTE: when performing related operations on more then one database
// table at a time, an error in any one of these transactions should undo
// or "rollback" all of the changes made

// 12) Create a transaction we can rollback
	GetDocument()->GetDatabase()->m_pWorkspace->BeginTrans();
	try
	{
		// perform transactions on several database tables/records
		wzdSet.Open();
		wzdSet.Update();
		// Success
		GetDocument()->GetDatabase()->m_pWorkspace->CommitTrans();
	}
	catch (CDaoException *e)
	{
		// An exception occurred, rollback the transaction
		GetDocument()->GetDatabase()->m_pWorkspace->Rollback();
 		AfxMessageBox(e->m_pErrorInfo->m_strDescription);
		e->Delete();
	}
	// since this was allocated on stack, returning will also close record set
	wzdSet.Close(); 

/////////////////////////////////////////////////////////////////////
// From: Visual C++ MFC Programming by Example by John E. Swanke
// Copyright (C) 1998 jeswanke. All rights reserved.
/////////////////////////////////////////////////////////////////////

⌨️ 快捷键说明

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