storage.cpp

来自「摘要:本文是笔者根据数据库编程经验」· C++ 代码 · 共 103 行

CPP
103
字号
// Storage.cpp: implementation of the CStorage class.

#include "stdafx.h"
#include "Storage.h"

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

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

CStorage::CStorage() : m_pConn(NULL), m_strConn(TEXT("")), m_iTimeOut(10)
{
}

CStorage::~CStorage()
{
	if(m_pConn != NULL) 
		delete m_pConn;
}

BOOL CStorage::Initialize()
{
	CoInitialize(0);  //Initializing COM

	m_pConn = new CAdoConnection;
	ASSERT(m_pConn);
	if(m_pConn == NULL) return FALSE;

	m_pConn->SetConnectTimeOut(m_iTimeOut);
	if(!m_pConn->Connect(m_strConn))
		return FALSE;

	return TRUE;
}

void CStorage::SetConnString(const CString& strConn)
{ 
	m_strConn  = strConn;
}

void CStorage::SetTimeCout(long iTimeOut)
{
	m_iTimeOut = iTimeOut;
}

CAdoConnection* CStorage::GetConn() const
{ 
	return m_pConn;
}

void CStorage::Close()
{
	if (m_pConn != NULL)
		m_pConn->Disconnect();

	CoUninitialize();  //Closing COM
}

BOOL CStorage::SaveEntity(CEntity* pEntity)
{
	if(pEntity == NULL) 
		return FALSE;

	m_pConn->BeginTrans();
	BOOL bSucceed = pEntity->Append(m_pConn);
	m_pConn->CommitTrans();

	return bSucceed;
}

BOOL CStorage::DeleteEntity(CEntity* pEntity)
{
	if(pEntity == NULL) return FALSE;
	
	m_pConn->BeginTrans();
	BOOL bSucceed = pEntity->Delete(m_pConn);
	m_pConn->CommitTrans();
	return bSucceed;
}

BOOL CStorage::ModifyEntity(CEntity* pEntity)
{
	if(pEntity == NULL) return FALSE;

	m_pConn->BeginTrans();
	BOOL bSucceed = pEntity->Modify(m_pConn);
	m_pConn->CommitTrans();

	return bSucceed;
}

BOOL CStorage::ReadEntity(CEntity* pEntity)
{
	if(pEntity == NULL) return FALSE;	

	return pEntity->Read(m_pConn);
}

⌨️ 快捷键说明

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