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

📄 adorecordset.cpp

📁 这是一个学校管理系统
💻 CPP
📖 第 1 页 / 共 5 页
字号:
/****************************************/
/*		ADO封装类设计AdoRecordSet 	    */
/*设计者:全佳营						*/
/*时间:2004.5.9						*/
/*Email:ghostman@tzc.edu.cn				*/
/*本ADO封装类是参考网友提供的设计的		*/
/****************************************/
#include "StdAfx.h"
#include ".\adorecordset.h"
#include <math.h>
#include <io.h>
/*------------------------------------------------
					CAdoRecordSet class
			 ------------------------------------------------*/
CAdoRecordSet::CAdoRecordSet(void)
{
	m_pConnection = NULL;
	m_SearchDirection = adSearchForward;
	m_pRecordset.CreateInstance("ADODB.Recordset");
	#ifdef _DEBUG
	if (m_pRecordset == NULL)
	{
		AfxMessageBox("RecordSet 对象创建失败! 请确认是否初始化了COM环境.");
	}
	#endif
	ASSERT(m_pRecordset != NULL);
}

CAdoRecordSet::CAdoRecordSet(CAdoConnection *pConnection)
{
	m_SearchDirection = adSearchForward;
	m_pConnection = pConnection;
	ASSERT(m_pConnection != NULL);
	m_pRecordset.CreateInstance("ADODB.Recordset");
	#ifdef _DEBUG
	if (m_pRecordset == NULL)
	{
		AfxMessageBox("RecordSet 对象创建失败! 请确认是否初始化了COM环境.");
	}
	#endif
	ASSERT(m_pRecordset != NULL);
}

CAdoRecordSet::~CAdoRecordSet(void)
{
	Release();
}
/*========================================================================
	Params:		
		- strSQL:		SQL语句, 表名, 存储过程调用或持久 Recordset 文件名.
		- CursorType:   可选. CursorTypeEnum 值, 确定打开 Recordset 时应该
					使用的游标类型. 可为下列常量之一.
		[常量]				[说明] 
		-----------------------------------------------
		adOpenForwardOnly	打开仅向前类型游标. 
		adOpenKeyset		打开键集类型游标. 
		adOpenDynamic		打开动态类型游标. 
		adOpenStatic		打开静态类型游标. 
		-----------------------------------------------
		- LockType:     可选, 确定打开 Recordset 时应该使用的锁定类型(并发)
					的 LockTypeEnum 值, 可为下列常量之一.
		[常量]				[说明] 
		-----------------------------------------------
		adLockReadOnly		只读 - 不能改变数据. 
		adLockPessimistic	保守式锁定 - 通常通过在编辑时立即锁定数据源的记录. 
		adLockOptimistic	开放式锁定 - 只在调用 Update 方法时才锁定记录. 
		adLockBatchOptimistic 开放式批更新 - 用于批更新模式(与立即更新模式
						相对). 
		-----------------------------------------------
		- lOption		可选. 长整型值, 用于指示 strSQL 参数的类型. 可为下
					列常量之一.
		[常量]				[说明] 
		-------------------------------------------------
		adCmdText			指示strSQL为命令文本, 即普通的SQL语句. 
		adCmdTable			指示ADO生成SQL查询返回以 strSQL 命名的表中的
						所有行. 
		adCmdTableDirect	指示所作的更改在strSQL中命名的表中返回所有行. 
		adCmdStoredProc		指示strSQL为存储过程. 
		adCmdUnknown		指示strSQL参数中的命令类型为未知. 
		adCmdFile			指示应从在strSQL中命名的文件中恢复保留(保存的)
						Recordset. 
		adAsyncExecute		指示应异步执行strSQL. 
		adAsyncFetch		指示在提取 Initial Fetch Size 属性中指定的初始
						数量后, 应该异步提取所有剩余的行. 如果所需的行尚未
						提取, 主要的线程将被堵塞直到行重新可用. 
		adAsyncFetchNonBlocking 指示主要线程在提取期间从未堵塞. 如果所请求
						的行尚未提取, 当前行自动移到文件末尾. 
==========================================================================*/
BOOL CAdoRecordSet::Open(LPCTSTR strSQL, long lOption, CursorTypeEnum CursorType, LockTypeEnum LockType)
{
	ASSERT(m_pConnection != NULL);
	ASSERT(m_pRecordset != NULL);
	ASSERT(AfxIsValidString(strSQL));

	if(strcmp(strSQL, _T("")) != 0)
	{
		m_strSQL = strSQL;
	}
	if (m_pConnection == NULL || m_pRecordset == NULL)
	{
		return FALSE;
	}

	if (m_strSQL.IsEmpty())
	{
		ASSERT(FALSE);
		return FALSE;
	}

	try
	{
		if (IsOpen()) Close();
		return SUCCEEDED(m_pRecordset->Open(_variant_t(LPCTSTR(m_strSQL)), 
											_variant_t((IDispatch*)m_pConnection->GetConnection(), true),
											CursorType, LockType, lOption));
	}
	catch (_com_error e)
	{
		m_errormessage.Format(_T("Warning: 打开记录集发生异常. 错误信息: %s; 文件: %s; 行: %d\n"), e.ErrorMessage(), __FILE__, __LINE__);
		AfxMessageBox(m_errormessage);
		TRACE(_T("%s\r\n"), GetLastError());
		return FALSE;
	}
}

/*========================================================================
	Name:		通过重新执行对象所基于的查询, 更新 Recordset 对象中的数据.
    ----------------------------------------------------------
	Params:		Options   可选. 指示影响该操作选项的位屏蔽. 如果该参数设置
		为 adAsyncExecute, 则该操作将异步执行并在它结束时产生 
		RecordsetChangeComplete 事件
    ----------------------------------------------------------
	Remarks:	通过重新发出原始命令并再次检索数据, 可使用 Requery 方法刷新
	来自数据源的 Recordset 对象的全部内容. 调用该方法等于相继调用 Close 和 
	Open 方法. 如果正在编辑当前记录或者添加新记录将产生错误.
==========================================================================*/
BOOL CAdoRecordSet::Requery(long Options)
{
	ASSERT(m_pRecordset != NULL);
	try
	{
		if (m_pRecordset != NULL) 
		{
			return (m_pRecordset->Requery(Options) == S_OK);
		}
	}
	catch (_com_error e)
	{
		m_errormessage.Format(_T("Warning: Requery 方法发生异常. 错误信息: %s; 文件: %s; 行: %d\n"), e.ErrorMessage(), __FILE__, __LINE__);
		AfxMessageBox(m_errormessage);
		return FALSE;
	} 
	return	FALSE; 
}

/*========================================================================
	Name:		从基本数据库刷新当前 Recordset 对象中的数据.	
    ----------------------------------------------------------
	Params:		AffectRecords:   可选, AffectEnum 值, 决定 Resync 方法所影
		响的记录数目, 可以为下列常量之一.
		[常量]				[说明]
		------------------------------------
		adAffectCurrent		只刷新当前记录. 
		adAffectGroup		刷新满足当前 Filter 属性设置的记录.只有将 Filter
						属性设置为有效预定义常量之一才能使用该选项. 
		adAffectAll			默认值.刷新 Recordset 对象中的所有记录, 包括由
						于当前 Filter 属性设置而隐藏的记录. 
		adAffectAllChapters 刷新所有子集记录. 

		ResyncValues:   可选, ResyncEnum 值. 指定是否覆盖基本值. 可为下列
					常量之一.
		[常量]				[说明] 
		------------------------------------
		adResyncAllValues	默认值. 覆盖数据, 取消挂起的更新. 
		adResyncUnderlyingValues 不覆盖数据, 不取消挂起的更新. 
    ----------------------------------------------------------
	Remarks:	使用 Resync 方法将当前 Recordset 中的记录与基本的数据库重新
	同步. 这在使用静态或仅向前的游标但希望看到基本数据库中的改动时十分有用.
	如果将 CursorLocation 属性设置为 adUseClient, 则 Resync 仅对非只读的 
	Recordset 对象可用.
	与 Requery 方法不同, Resync 方法不重新执行 Recordset 对象的基本的命令, 
	基本的数据库中的新记录将不可见.
==========================================================================*/
BOOL CAdoRecordSet::Resync(AffectEnum AffectRecords, ResyncEnum ResyncValues)
{
	ASSERT(m_pRecordset != NULL);
	try
	{
		if (m_pRecordset != NULL) 
		{
			return (m_pRecordset->Resync(AffectRecords, ResyncValues) == S_OK);
		}
	}
	catch (_com_error e)
	{
		m_errormessage.Format(_T("Warning: Resync 方法发生异常. 错误信息: %s; 文件: %s; 行: %d\n"), e.ErrorMessage(), __FILE__, __LINE__);
		AfxMessageBox(m_errormessage);
		return FALSE;
	} 
	return	FALSE; 
}


/*========================================================================
	Name:		取消执行挂起的异步 Execute 或 Open 方法的调用.
	-----------------------------------------------------
	Remarks:	使用 Cancel 方法终止执行异步 Execute 或 Open 方法调用(即通
		过 adAsyncConnect、adAsyncExecute 或 adAsyncFetch 参数调用的方法).
		如果在试图终止的方法中没有使用 adAsyncExecute, 则 Cancel 将返回运行
		时错误.
==========================================================================*/
BOOL CAdoRecordSet::Cancel()
{
	ASSERT(m_pRecordset != NULL);
	try
	{
		return m_pRecordset->Cancel();
	}
	catch (_com_error e)
	{
		m_errormessage.Format(_T("Warning: Cancel发生异常. 错误信息: %s; 文件: %s; 行: %d\n"), e.ErrorMessage(), __FILE__, __LINE__);
		AfxMessageBox(m_errormessage);
		return FALSE;
	} 
	return FALSE;
}

/*========================================================================
	Name:		关闭打开的对象及任何相关对象.
	-----------------------------------------------------
	Remarks:	使用 Close 方法可关闭 Recordset 对象以便释放所有关联的系统
		资源. 关闭对象并非将它从内存中删除, 可以更改它的属性设置并且在此后
		再次打开. 要将对象从内存中完全删除, 可将对象变量设置为 NULL.
			如果正在立即更新模式下进行编辑, 调用Close方法将产生错误,应首先
		调用 Update 或 CancelUpdat 方法. 如果在批更新期间关闭 Recordset 对
		象, 则自上次 UpdateBatch 调用以来所做的修改将全部丢失.
			如果使用 Clone 方法创建已打开的 Recordset 对象的副本, 关闭原始
		Recordset或其副本将不影响任何其他副本.
==========================================================================*/
void CAdoRecordSet::Close()
{
	try
	{
		if (m_pRecordset != NULL && m_pRecordset->State != adStateClosed)
		{
			if (GetEditMode() == adEditNone) CancelUpdate();
			m_pRecordset->Close();
		}
	}
	catch (const _com_error& e)
	{
		m_errormessage.Format(_T("Warning: 关闭记录集发生异常. 错误信息: %s; 文件: %s; 行: %d\n"), e.ErrorMessage(), __FILE__, __LINE__);
		AfxMessageBox(m_errormessage);
	}
}

/*========================================================================
	Name:	关闭连接并释放对象.
	-----------------------------------------------------
	Remarks: 关闭连接并释放CAdoRecordSet对象, 这样基本上从内容中完全清除了
		CAdoRecordSet对象.
==========================================================================*/
void CAdoRecordSet::Release()
{
	if (IsOpen()) Close();
	m_pRecordset.Release();
	m_pRecordset = NULL;
}

/*========================================================================
	Remarks:	开始添加新的纪录. 
==========================================================================*/
BOOL CAdoRecordSet::AddNew()
{
	ASSERT(m_pRecordset != NULL);
	try
	{
		if (m_pRecordset != NULL) 
		{
			if (m_pRecordset->AddNew() == S_OK)
			{
				return TRUE;
			}
		}
	}
	catch (_com_error e)
	{
		m_errormessage.Format(_T("Warning: AddNew 方法发生异常. 错误信息: %s; 文件: %s; 行: %d\n"), e.ErrorMessage(), __FILE__, __LINE__);
		AfxMessageBox(m_errormessage);
		return FALSE;
	} 
	return	FALSE;
}

/*========================================================================
	Remarks:	在调用 AddNew 等方法后, 调用此方法完成更新或修改.
==========================================================================*/
BOOL CAdoRecordSet::Update()
{
	ASSERT(m_pRecordset != NULL);
	try
	{
		if (m_pRecordset != NULL) 
		{
			if (m_pRecordset->Update() == S_OK)
			{
				return TRUE;
			}
		}
	}
	catch (_com_error e)
	{
		m_errormessage.Format(_T("Warning: Update 方法发生异常. 错误信息: %s; 文件: %s; 行: %d\n"), e.ErrorMessage(), __FILE__, __LINE__);
		AfxMessageBox(m_errormessage);
	}
	
	CancelUpdate();
	return	FALSE;
}

/*========================================================================
	Name:		将所有挂起的批更新写入磁盘.
    ----------------------------------------------------------
	Params:		AffectRecords   可选, AffectEnum 值. 决定 UpdateBatch 方法
		所影响的记录数目.可以为如下常量之一.
		[常量]				[说明] 
		------------------------------------
		adAffectCurrent		只写入当前记录的挂起更改. 
		adAffectGroup		写入满足当前 Filter 属性设置的记录所发生的挂起
			更改. 必须将 Filter 属性设置为某个有效的预定义常量才能使用该选项. 
		adAffectAll			(默认值). 写入 Recordset 对象中所有记录的挂起
						更改, 包括由于当前 Filter 属性设置而隐藏的任何记录. 
		adAffectAllChapters 写入所有子集的挂起更改. 
    ----------------------------------------------------------
	Remarks:	按批更新模式修改 Recordset 对象时, 使用 UpdateBatch 方法可
	将 Recordset 对象中的所有更改传递到基本数据库.
	如果 Recordset 对象支持批更新, 那么可以将一个或多个记录的多重更改缓存在
	本地, 然后再调用 UpdateBatch 方法. 如果在调用 UpdateBatch 方法时正在编
	辑当前记录或者添加新的记录, 那么在将批更新传送到提供者之前, ADO 将自动
	调用 Update 方法保存对当前记录的所有挂起更改.
	   只能对键集或静态游标使用批更新.
==========================================================================*/
BOOL CAdoRecordSet::UpdateBatch(AffectEnum AffectRecords)
{
	ASSERT(m_pRecordset != NULL);
	try
	{
		if (m_pRecordset != NULL) 
		{
			return (m_pRecordset->UpdateBatch(AffectRecords) == S_OK);
		}
	}
	catch (_com_error e)
	{
		m_errormessage.Format(_T("Warning: UpdateBatch 方法发生异常. 错误信息: %s; 文件: %s; 行: %d\n"), e.ErrorMessage(), __FILE__, __LINE__);
		AfxMessageBox(m_errormessage);
		return FALSE;
	} 
	return	FALSE; 
}

/*========================================================================
	Name:		取消在调用 Update 方法前对当前记录或新记录所作的任何更改.
	-----------------------------------------------------
	Remarks:	使用 CancelUpdate 方法可取消对当前记录所作的任何更改或放弃
	新添加的记录. 在调用 Update 方法后将无法撤消对当前记录或新记录所做的更
	改, 除非更改是可以用 RollbackTrans 方法回卷的事务的一部分, 或者是可以
	用 CancelBatch 方法取消的批更新的一部分.
		如果在调用 CancelUpdate 方法时添加新记录, 则调用 AddNew 之前的当前
	记录将再次成为当前记录.
		如果尚未更改当前记录或添加新记录, 调用 CancelUpdate 方法将产生错误.
==========================================================================*/
BOOL CAdoRecordSet::CancelUpdate()
{
	ASSERT(m_pRecordset != NULL);
	try
	{
		if (m_pRecordset != NULL) 
		{
			if (GetEditMode() == adEditNone || m_pRecordset->CancelUpdate() == S_OK)
			{
				return TRUE;
			}
		}
	}
	catch (_com_error e)
	{
		m_errormessage.Format(_T("Warning: CancelUpdate 方法发生异常. 错误信息: %s; 文件: %s; 行: %d\n"), e.ErrorMessage(), __FILE__, __LINE__);
		AfxMessageBox(m_errormessage);
		return FALSE;
	} 
	return FALSE;
}

/*========================================================================
	Name:		取消挂起的批更新.
	-----------------------------------------------------
	Params:		AffectRecords   可选的 AffectEnum 值, 决定CancelBatch 方法
		所影响记录的数目, 可为下列常量之一: 
		[常量]			[说明] 

⌨️ 快捷键说明

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