📄 connectionpool.cpp
字号:
#include "stdafx.h"
#include "StrDefines.h"
#include "ConnectionPool.h"
CConnectionPool::CConnectionPool()
{
}
CConnectionPool::~CConnectionPool()
{
for(int n = 0 ; n < m_pConnections.GetSize(); n++)
{
if(m_pConnections[n]->m_DataBase.IsOpen())
m_pConnections[n]->m_DataBase.Close();
delete m_pConnections[n];
}//for(int n = 0 ; n < m_pConnections.GetSize(); n++)
m_pConnections.RemoveAll();
}//CConnectionPool::~CConnectionPool()
// Function : AddConnection
// Purpose : Keeps a connection in the internal structur until the whole structure is destroyed
// Param : DBCon a connection that is allready initialized to a database
// Return :
void CConnectionPool::AddConnection(CDBConnection *DBCon)
{
m_pConnections.Add(DBCon);
}//void CConnectionPool::AddConnection(CDBConnection *DBCon)
// Function : AddConnection
// Purpose : Creates a new connection and adds it to the internal structure
// Param : DSN, user name, user password, if the database logon dialog should be run
// Return : TRUE if connection is established, otherwise FALSE
BOOL CConnectionPool::AddConnection(CString DSN,CString sUser,CString sPassword,BOOL UseLogonDlg)
{
CDBConnection *pDBCon = new CDBConnection();
if(pDBCon->ConnectToDataBase(DSN,sUser,sPassword,UseLogonDlg))
AddConnection(pDBCon);
else
return FALSE;
return TRUE;
}//BOOL CConnectionPool::AddConnection(CString DSN,CString sUser,CString sPassword,BOOL UseLogonDlg)
// Function : GetConnection
// Purpose : Finds and returns a connection to a database
// Param : The DSN that specifies the database connection
// Return : A connection class
CDBConnection* CConnectionPool::GetConnection(CString sDSN)
{
BOOL Found = FALSE;
int i = 0;
if (m_pConnections.GetSize() < 1)
return NULL;
while(!Found)
{
CDBConnection* pCon = m_pConnections.GetAt(i);
if(pCon->m_sConnectedDSN.CompareNoCase(sDSN) == 0)
return pCon;
else
if(i >= m_pConnections.GetSize() -1 )
return NULL;
i++;
}//while(!Found)
return NULL;
}//CDBConnection* CConnectionPool::GetConnection(CString sDSN)
// Function : GetConnection
// Purpose : Finds and returns a connection to a database
// Param : The index of the requested database connection
// Return : A connection class
CDBConnection* CConnectionPool::GetConnection(int index)
{
ASSERT(index < m_pConnections.GetSize());
return m_pConnections.GetAt(index);
}//CDBConnection* CConnectionPool::GetConnection(int index)
// Function : GetNumConnections
// Purpose : Retrives number of database connections held in the structure
// Param : none
// Return : number of DB connections
int CConnectionPool::GetNumConnections()
{
return m_pConnections.GetSize();
}//int CConnectionPool::GetNumConnections()
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -