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

📄 storage.cpp

📁 摘要:本文是笔者根据数据库编程经验
💻 CPP
字号:
// 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 + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -