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

📄 adotable.cpp

📁 手机编程之接收发送短信
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// AdoTable.cpp: implementation of the CAdoTable class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "AdoTable.h"
#include "dumperr.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////


//-------------------------------------------------------------------------------------------
//	函数名:CAdoTable::CAdoTable
//	函数功能:构造函数
//	返回值:
//	参数说明:
//		const char* sTabName : 本表格在数据库中的名称
//		const char*sConString : 连接字符串
//-------------------------------------------------------------------------------------------
CAdoTable::CAdoTable(const char * sConString)
{
	
	memset(sSql,'\0',MAXSTRLEN);
	memset(sError,'\0',ERRORSTR_LEN);
	memset(sTableName,'\0',TABLESTRLEN);
	memset(sConnectString,'\0',MAXSTRLEN);

	strcpy(sConnectString,sConString);

	::CoInitialize(NULL);

	pConnect.CreateInstance(__uuidof(Connection)); 
	pRecordSet.CreateInstance(__uuidof(Recordset));
    pConnect->Mode=adModeReadWrite;


}


//-------------------------------------------------------------------------------------------
//	函数名:CAdoTable::~CAdoTable
//	函数功能:析构函数
//-------------------------------------------------------------------------------------------
CAdoTable::~CAdoTable()
{
	Disconnect();

    pRecordSet.Release();
	pConnect.Release();

	pConnect = NULL;
	pRecordSet = NULL;
	::CoUninitialize();
}

//-------------------------------------------------------------------------------------------
//	函数名:CAdoTable::ConnectDB
//	函数功能:连接到数据库
//	返回值:参看EV_ERR系列宏定义,仅当返回EV_ERR_OK的时候正确
//	参数说明:
//		DWORD dwFlag : 连接参数,指定对该表格的操作方法,默认是adModeReadWrite
//                     可选:
//						adModeRead :只读
//						adModeWrite :只写
//						adModeReadWrite :读、写
//						adModeShareDenyRead :Prevents others from opening connection with read permissions. 
//						adModeShareDenyWrite:Prevents others from opening connection with write permissions. 
//						adModeShareExclusive:Prevents others from opening connection. 
//						adModeShareDenyNone: Prevents others from opening connection with any permissions. 
//-------------------------------------------------------------------------------------------
int CAdoTable::ConnectDB(DWORD dwFlag /*= adModeReadWrite*/ )
{
	try
	{    
		pConnect->Open(_bstr_t(sConnectString),
			_bstr_t(""),_bstr_t(""),adConnectUnspecified);

	}catch(_com_error &e){
		dump_com_error ( e, sError, ERRORSTR_LEN, "ConnectDB()连接数据库错误!" );
		return EV_ERR_CONNECTION;
	}

	return EV_ERR_OK;
	
}

//-------------------------------------------------------------------------------------------
//	函数名:CAdoTable::Disconnect
//	函数功能:断开与数据库的连接,释放所有分配的资源
//	返回值:参看EV_ERR系列宏定义,仅当返回EV_ERR_OK的时候正确
//-------------------------------------------------------------------------------------------
int CAdoTable::Disconnect( void )
{
	try
	{
		if(pRecordSet->State==adStateOpen) {
			pRecordSet->Close();
		}
		
	}catch(_com_error &e)
	{
		dump_com_error( e, sError, ERRORSTR_LEN, "Disconnect()断开连接数据库错误!" );
	}

	try
	{
		if(pConnect->State==adStateOpen) {
				pConnect->Close();
		}
	}catch(_com_error &e){
		dump_com_error( e, sError, ERRORSTR_LEN, "Disconnect()断开连接数据库错误!" );
		return EV_ERR_DISCONNECTION;
	}

	return EV_ERR_OK;
	
}

//-------------------------------------------------------------------------------------------
//	函数名:CAdoTable::GetErrorStr
//	函数功能:获得当前错误信息字符串
//	参数说明:
//		char *sError : 返回当前错误信息字符串
//-------------------------------------------------------------------------------------------
char * CAdoTable::GetErrorStr( void )
{
	return sError;
}


//-------------------------------------------------------------------------------------------
//	函数名:CAdoTable::GetRecordsetNumber
//	函数功能:取当前表格中记录的个数
//	返回值:参看EV_ERR系列宏定义,仅当返回EV_ERR_OK的时候正确
//	参数说明:
//		int& number : 返回表中记录个数
//-------------------------------------------------------------------------------------------
int CAdoTable::GetRecordsetNumber(int& number)
{
	number=0;
	
	try
	{
		if( pRecordSet->BOF || pRecordSet->adoEOF) {
			return EV_ERR_OK;
		}
		
#if 1
		pRecordSet->MoveLast();
		while(!pRecordSet->BOF)
		{
			number++;
			pRecordSet->MovePrevious();
		}
		pRecordSet->MoveFirst();
#else

		if(pRecordSet->State==adStateOpen) {
			pRecordSet->Close();
		}

		sprintf(sSql,"select count(*) from %s", sTableName );

		pRecordSet->Open(_bstr_t(sSql), _variant_t((IDispatch*)(pConnect)),
			adOpenDynamic,adLockOptimistic, adCmdText);
		
		number = long( pRecordSet->Fields->Item[0L]->Value );
		// result=long(pRecordSet->Fields->Item[fieldname]->Value);
#endif		
		
	}catch(_com_error &e){
		dump_com_error( e, sError, ERRORSTR_LEN, "获取表格记录总数出错!" );
		return EV_ERR_COM;
	}
	
	return EV_ERR_OK;
}

//-------------------------------------------------------------------------------------------
//	函数名:CAdoTable::IsBegin
//	函数功能:判断表格光标是不是在最前(adoBOF)
//	返回值:TRUE  是;FALSE  否
//-------------------------------------------------------------------------------------------
BOOL CAdoTable::IsBegin( void )
{
	return ( pRecordSet->BOF );
}


//-------------------------------------------------------------------------------------------
//	函数名:CAdoTable::IsEnd
//	函数功能:判断表格光标是不是在最后(adoBOF)
//	返回值:TRUE  是;FALSE  否
//-------------------------------------------------------------------------------------------
BOOL CAdoTable::IsEnd( void )
{
	return ( pRecordSet->adoEOF );
}

//-------------------------------------------------------------------------------------------
//	函数名:CAdoTable::MoveNext
//	函数功能:在表格中往下移动一个记录
//	返回值:参看EV_ERR系列宏定义,仅当返回EV_ERR_OK的时候正确
//  备注:在MoveNext()之前请先调用IsEnd()判断是否已经到达表格末端
//-------------------------------------------------------------------------------------------
int CAdoTable::MoveNext()
{
	try
	{	
		pRecordSet->MoveNext();

	}catch(_com_error &e){

		dump_com_error( e, sError, ERRORSTR_LEN, "MoveNext出错!" );
		return EV_ERR_COM;
	}
	
	return EV_ERR_OK;
}

//-------------------------------------------------------------------------------------------
//	函数名:CAdoTable::MovePre
//	函数功能:在表格中往前移动一个记录
//	返回值:参看EV_ERR系列宏定义,仅当返回EV_ERR_OK的时候正确
//  备注:在MovePre()之前请先调用IsBegin()判断是否已经是表格最前面的记录
//-------------------------------------------------------------------------------------------
int CAdoTable::MovePre( void )
{
	try
	{
		pRecordSet->MovePrevious();

	}catch(_com_error &e){

		dump_com_error( e, sError, ERRORSTR_LEN, "MovePre出错!" );
		return EV_ERR_COM;
	}
	
	return EV_ERR_OK;
	
}


//-------------------------------------------------------------------------------------------
//	函数名:CAdoTable::MoveFirst
//	函数功能:将光标往前移动到第一条记录
//	返回值:参看EV_ERR系列宏定义,仅当返回EV_ERR_OK的时候正确
//  备注:在MoveFirst()之前请先调用IsBegin()判断是否已经是表格最前面的记录
//-------------------------------------------------------------------------------------------
int CAdoTable::MoveFirst()
{
	try
	{
		pRecordSet->MoveFirst();

	}catch(_com_error &e){

		dump_com_error( e, sError, ERRORSTR_LEN, "MoveFirst出错!" );
		return EV_ERR_COM;
	}
	
	return EV_ERR_OK;
}


//-------------------------------------------------------------------------------------------
//	函数名:CAdoTable::MoveLast
//	函数功能:将光标往前移动到最后一条记录
//	返回值:参看EV_ERR系列宏定义,仅当返回EV_ERR_OK的时候正确
//  备注:在MoveLast()之前请先调用IsEnd()判断是否已经到达表格末端
//-------------------------------------------------------------------------------------------
int CAdoTable::MoveLast()
{
	try
	{
		pRecordSet->MoveLast();

	}catch(_com_error &e){

		dump_com_error( e, sError, ERRORSTR_LEN, "MoveLast出错!" );
		return EV_ERR_COM;
	}
	
	return EV_ERR_OK;
	
}


⌨️ 快捷键说明

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