BOOL CADODatabase::Open(LPCTSTR lpstrConnection, LPCTSTR lpstrUserID, LPCTSTR lpstrPassword)
{
HRESULT hr = S_OK;
if(IsOpen())
Close();
if(strcmp(lpstrConnection, _T("")) != 0)
m_strConnection = lpstrConnection;
ASSERT(!m_strConnection.IsEmpty());
try
{
if(m_nConnectionTimeout != 0)
m_pConnection->PutConnectionTimeout(m_nConnectionTimeout);
hr = m_pConnection->Open(_bstr_t(m_strConnection), _bstr_t(lpstrUserID), _bstr_t(lpstrPassword), NULL);
return hr == S_OK;
}
catch(_com_error &e)
{
dump_com_error(e);
return FALSE;
}
}
BOOL CADODatabase::ConnectAccess(LPCTSTR lpstrDatabaseName,LPCTSTR lpstrPassword, long lOptions)
{
CString strConnect;
strConnect.Format("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=%s",lpstrDatabaseName);
if (lpstrPassword != _T(""))
{
strConnect.Format("Jet OLEDB:Database Password=%s;",lpstrPassword);
}
return Open(LPCTSTR(strConnect));
}
BOOL CADODatabase::ConnectSQLServer(LPCTSTR lpstrDatabaseServer, LPCTSTR lpstrDatabaseName, LPCTSTR lpstrUserID, LPCTSTR lpstrPassword)
{
CString strConnect;
strConnect.Format("Provider=SQLOLEDB.1; Data Source=%s; Initial Catalog=%s",lpstrDatabaseServer,lpstrDatabaseName);
return Open(LPCTSTR(strConnect), lpstrUserID, lpstrPassword);
}
BOOL CADODatabase::Execute(LPCTSTR lpstrExec)
{
ASSERT(m_pConnection != NULL);
ASSERT(strcmp(lpstrExec, _T("")) != 0);
_variant_t vRecords;
m_nRecordsAffected = 0;
try
{
m_pConnection->CursorLocation = adUseClient;
m_pConnection->Execute(_bstr_t(lpstrExec), &vRecords, adExecuteNoRecords);
m_nRecordsAffected = vRecords.iVal;
return TRUE;
}
catch(_com_error &e)
{
dump_com_error(e);
return FALSE;
}
}
BOOL CADODatabase::ExecuteSqlFile(LPCTSTR lpstrFileName)
{
CStdioFileEx pFile;
if(!pFile.Open(lpstrFileName,CFile::modeRead | CFile::typeText))
{
return FALSE;
}
CString strSql,strRead;
strSql.Empty();
while(pFile.ReadString(strRead))
{
if(strRead.Trim().CompareNoCase(_T("GO"))==0)
{
if(!strSql.IsEmpty())
{
if(!Execute(strSql))
return FALSE;
strSql.Empty();
}
}
else
{
strSql+=_T(" ")+strRead;
}
}
return TRUE;
}