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

📄 ado.cpp

📁 一个自己修改过的ado类
💻 CPP
📖 第 1 页 / 共 2 页
字号:
//
//  文件:   Ado.cpp
//

#include "ado.h"

#define DelLogDebugEvent(a, b, c, d, e, f) TRACE("\n%s(%d):%s\n",__FILE__,__LINE__,(c));

DWORD CADODatabase::GetRecordCount(_RecordsetPtr m_pRs)
{
	DWORD numRows = 0;
	
	numRows = m_pRs->GetRecordCount();

	if(numRows == -1)
	{
		if(m_pRs->EndOfFile != VARIANT_TRUE)
			m_pRs->MoveFirst();

		while(m_pRs->EndOfFile != VARIANT_TRUE)
		{
			numRows++;
			m_pRs->MoveNext();
		}
		if(numRows > 0)
			m_pRs->MoveFirst();
	}
	return numRows;
}

bool CADODatabase::Open(LPCTSTR lpstrConnection, LPCTSTR lpstrUserID, LPCTSTR lpstrPassword)
{
	HRESULT hr = S_OK;

	if(IsOpen())
		Close();

	if(strcmp(lpstrConnection, _T("")) != 0)
		m_strConnection = lpstrConnection;

    if (m_strConnection.IsEmpty())
    {
        CString strEvent;
        strEvent.Format("- Input connection string is empty.\n");
        DelLogDebugEvent("CADODatabase", "Open", strEvent, eDELIMPORTANCE_LEVEL_HIGHEST, __FILE__, __LINE__);
		return false;
    }

	try
	{
		hr = m_pConnection->Open(_bstr_t(m_strConnection), _bstr_t(lpstrUserID), _bstr_t(lpstrPassword), NULL);
		//hr = m_pConnection->Open(_bstr_t(m_strConnection), "", "", NULL);
		return hr == S_OK;
	}
	catch(_com_error &e)
	{
		dump_com_error(e);
	}
	return false;
}

void CADODatabase::dump_com_error(_com_error &e)
{
	CString ErrorStr;
	
	
	_bstr_t bstrSource(e.Source());
	_bstr_t bstrDescription(e.Description());
	ErrorStr.Format( "CADODataBase Error\n\tCode = %08lx\n\tCode meaning = %s\n\tSource = %s\n\tDescription = %s\n",
		e.Error(), e.ErrorMessage(), (LPCSTR)bstrSource, (LPCSTR)bstrDescription );
	m_strLastError = _T("Connection String = " + GetConnectionString() + '\n' + ErrorStr);

//	#ifdef _DEBUG
//		AfxMessageBox( ErrorStr, MB_OK | MB_ICONERROR );
//	#endif	

	TRACE("%s",COleDateTime::GetCurrentTime().Format("%Y-%m-%d %H:%M:%S"));
//	ASSERT(FALSE);
    DelLogDebugEvent("CADODatabase", "dump_com_error", ErrorStr, eDELIMPORTANCE_LEVEL_HIGH, __FILE__, __LINE__);
}

bool CADODatabase::IsOpen()
{
	if(m_pConnection )
		return m_pConnection->GetState() != adStateClosed;
	return false;
}

void CADODatabase::Close()
{
	if(IsOpen())
		m_pConnection->Close();
}


CADORecordset::CADORecordset(CADODatabase* pAdoDatabase)
{
	m_pRecordset = NULL;
	m_pCmd = NULL;
	m_strQuery = _T("");
	m_pRecordset.CreateInstance(__uuidof(Recordset));
	m_pCmd.CreateInstance(__uuidof(Command));
	m_nEditStatus = CADORecordset::dbEditNone;
	m_nSearchDirection = CADORecordset::searchForward;

	m_pConnection = pAdoDatabase->GetActiveConnection();
}

bool CADORecordset::Open(_ConnectionPtr mpdb, LPCTSTR lpstrExec, int nOption)
{	
	Close();
	
	if(strcmp(lpstrExec, _T("")) != 0)
		m_strQuery = lpstrExec;

	m_strQuery.TrimLeft();

    if (m_strQuery.IsEmpty())
    {
        CString strEvent;
        strEvent.Format("- Input query string is empty.\n");
        DelLogDebugEvent("CADORecordset", "Open", strEvent, eDELIMPORTANCE_LEVEL_HIGHEST, __FILE__, __LINE__);
		return false;
    }
	
	BOOL bIsSelect = m_strQuery.Mid(0, (int)strlen("Select ")).CompareNoCase("select ") == 0;

	try
	{
        // Cursor Location
        if(m_nCursorLocation == useNone)
        {
            if (nOption == openStoredProc)
                m_nCursorLocation = useServer;
            else
                m_nCursorLocation = useClient;
        }
        
        // Cursor Type
        if(m_nCursorType == cursorUnspecified)
        {
            if (nOption == openQuery)
                m_nCursorType = cursorStatic;
            else if (nOption == openStoredProc)
                m_nCursorType = cursorForwardOnly;
            else
                m_nCursorType = cursorDynamic; // default to dynamic
        }
        
        // Lock Type
        if(m_nLockType == lockUnspecified)
        {
            if (nOption == openStoredProc)
                m_nLockType = lockReadOnly;
            else
                m_nLockType = lockOptimistic;
        }
        
        
        m_pRecordset->CursorLocation = (enum CursorLocationEnum)m_nCursorLocation;
        
        if(bIsSelect || nOption == openQuery)
		{
            m_pRecordset->Open((LPCSTR)m_strQuery, _variant_t((IDispatch*)mpdb, true), 
								(enum ADOCG::CursorTypeEnum)m_nCursorType, 
								(enum ADOCG::LockTypeEnum)m_nLockType,
                                adCmdUnknown);
		}
        else if(nOption == openTable)
		{
            m_pRecordset->Open((LPCSTR)m_strQuery, _variant_t((IDispatch*)mpdb, true), 
                                (enum ADOCG::CursorTypeEnum)m_nCursorType,
								(enum ADOCG::LockTypeEnum)m_nLockType,
                                adCmdTable);
		}
        else if(nOption == openStoredProc)
        {
            m_pRecordset->Open((LPCSTR)m_strQuery, _variant_t((IDispatch*)mpdb, true),
                                (enum ADOCG::CursorTypeEnum)m_nCursorType,
                                (enum ADOCG::LockTypeEnum)m_nLockType,
                                adCmdText);
        }
        else
        {
            CString strEvent;
            strEvent.Format("- Invalid option to open a recordset (%d).\n", nOption);
            DelLogDebugEvent("CADORecordset", "Open", strEvent, eDELIMPORTANCE_LEVEL_HIGHEST, __FILE__, __LINE__);
            return false;
        }
    }
	catch(_com_error &e)
	{
		dump_com_error(e);
		return false;
	}

	return m_pRecordset != NULL;
}

bool CADORecordset::Open(LPCTSTR lpstrExec, int nOption)
{
    if (m_pConnection == NULL)
    {
        CString strEvent;
        strEvent.Format("- Connection is NULL.\n");
        DelLogDebugEvent("CADORecordset", "Open", strEvent, eDELIMPORTANCE_LEVEL_HIGHEST, __FILE__, __LINE__);
		return false;
    }
	
	return Open(m_pConnection, lpstrExec, nOption);
}

bool CADORecordset::GetFieldValue(LPCTSTR lpFieldName, double& dbValue)
{	
	double val = (double)NULL;
	_variant_t vtFld;
	
	vtFld = m_pRecordset->Fields->GetItem(lpFieldName)->Value;
	if(vtFld.vt != VT_NULL)
		val = vtFld.dblVal;
	dbValue = val;
	return true;
}


bool CADORecordset::GetFieldValue(int nIndex, double& dbValue)
{	
	double val = (double)NULL;
	_variant_t vtFld;
	_variant_t vtIndex;

	vtIndex.vt = VT_I2;
	vtIndex.iVal = nIndex;
	
	vtFld = m_pRecordset->Fields->GetItem(vtIndex)->Value;
	if(vtFld.vt != VT_NULL)
		val = vtFld.dblVal;
	dbValue = val;
	return true;
}
bool CADORecordset::GetFieldValue(LPCTSTR lpFieldName, long& lValue)
{
	long val = (long)NULL;
	_variant_t vtFld;
	
	vtFld = m_pRecordset->Fields->GetItem(lpFieldName)->Value;
	if(vtFld.vt != VT_NULL)
		val = vtFld.lVal;
	lValue = val;
	return true;
}

bool CADORecordset::GetFieldValue(int nIndex, long& lValue)
{
	long val = (long)NULL;
	_variant_t vtFld;
	_variant_t vtIndex;
	
	vtIndex.vt = VT_I2;
	vtIndex.iVal = nIndex;
	vtFld = m_pRecordset->Fields->GetItem(vtIndex)->Value;
	if(vtFld.vt != VT_NULL)
		val = vtFld.lVal;
	lValue = val;
	return true;
}

bool CADORecordset::GetFieldValue(LPCTSTR lpFieldName, BYTE& byValue)
{
	long lTemp;
	GetFieldValue(lpFieldName, lTemp);
	byValue = (BYTE)lTemp;
	return true;
}

bool CADORecordset::GetFieldValue(int nIndex, BYTE& byValue)
{
	long lTemp;
	GetFieldValue(nIndex, lTemp);
	byValue = (BYTE)lTemp;
	return true;
}



bool CADORecordset::GetFieldValue(LPCTSTR lpFieldName, int& nValue)
{
	int val = NULL;
	_variant_t vtFld;
	
	vtFld = m_pRecordset->Fields->GetItem(lpFieldName)->Value;
	switch(vtFld.vt)
	{
	case VT_I2:
		val = vtFld.iVal;
		break;
	case VT_BOOL:
		val = vtFld.boolVal;
	case VT_NULL:
	case VT_EMPTY:
		break;
	default:
		nValue = 0;
		return false;
	}	
	nValue = val;
	return true;
}

bool CADORecordset::GetFieldValue(int nIndex, int& nValue)
{
	int val = (int)NULL;
	_variant_t vtFld;
	_variant_t vtIndex;
	
	vtIndex.vt = VT_I2;
	vtIndex.iVal = nIndex;
	vtFld = m_pRecordset->Fields->GetItem(vtIndex)->Value;
	switch(vtFld.vt)
	{
	case VT_I2:
		val = vtFld.iVal;
		break;
	case VT_NULL:
	case VT_EMPTY:
		val = 0;
		break;
	default:
		return false;
	}	
	nValue = val;
	return true;
}

bool CADORecordset::GetFieldValue(LPCTSTR lpFieldName, CString& strValue)
{
	CString str = _T("");
	_variant_t vtFld;
	
	vtFld = m_pRecordset->Fields->GetItem(lpFieldName)->Value;
	switch(vtFld.vt) 
	{
	case VT_BSTR:
		str = vtFld.bstrVal;
		break;
	case VT_I4:
		str = IntToStr(vtFld.iVal);
		break;
	case VT_DATE:
		{
			COleDateTime dt(vtFld);

			str = dt.Format("%Y-%m-%d %H:%M:%S");
		}
		break;
	case VT_EMPTY:
	case VT_NULL:
		break;
	default:
		strValue.Empty();
		return false;
	}
	strValue = str;
	return true;
}

bool CADORecordset::GetFieldValue(LPCTSTR lpFieldName, CString& strValue, UINT *puLen)
{
	long nSize = m_pRecordset->GetFields()->GetItem(lpFieldName)->ActualSize;
	if(nSize > 0)
	{
		_variant_t	varBLOB;
		varBLOB = m_pRecordset->GetFields()->GetItem(lpFieldName)->GetChunk(nSize);
		if(varBLOB.vt == (VT_ARRAY | VT_UI1))
		{
			BYTE *pBuffer = (BYTE *)strValue.GetBufferSetLength(nSize);

			char *pBuf = NULL;
			SafeArrayAccessData(varBLOB.parray,(void **)&pBuf);
			memcpy(pBuffer, pBuf, nSize);	
			SafeArrayUnaccessData(varBLOB.parray);

			strValue.ReleaseBuffer(nSize);
			
			*puLen = nSize;
		}
	}
	
	return true;
}

bool CADORecordset::GetFieldValue(int nIndex, CString& strValue)
{
	CString str = _T("");
	_variant_t vtFld;
	_variant_t vtIndex;

	vtIndex.vt = VT_I2;
	vtIndex.iVal = nIndex;
	
	vtFld = m_pRecordset->Fields->GetItem(vtIndex)->Value;
	switch(vtFld.vt) 
	{
	case VT_BSTR:
		str = vtFld.bstrVal;
		break;
	case VT_DATE:
		{
			COleDateTime dt(vtFld);
			
			str = dt.Format("%Y-%m-%d %H:%M:%S");
		}
		break;
	case VT_EMPTY:
	case VT_NULL:
		break;
	default:
		strValue.Empty();
		return false;
	}
	strValue = str;
	return true;
}

bool CADORecordset::GetFieldValue(LPCTSTR lpFieldName, COleDateTime& time)
{
	_variant_t vtFld;
	
	vtFld = m_pRecordset->Fields->GetItem(lpFieldName)->Value;
	switch(vtFld.vt) 
	{
	case VT_DATE:
		{
			COleDateTime dt(vtFld);
			time = dt;
		}
		break;
	case VT_EMPTY:
	case VT_NULL:
		break;
	default:
		return false;
	}
	return true;
}

bool CADORecordset::GetFieldValue(int nIndex, COleDateTime& time)
{
	_variant_t vtFld;
	_variant_t vtIndex;
	
	vtIndex.vt = VT_I2;
	vtIndex.iVal = nIndex;
	
	vtFld = m_pRecordset->Fields->GetItem(vtIndex)->Value;
	switch(vtFld.vt) 
	{
	case VT_DATE:
		{
			COleDateTime dt(vtFld);
			time = dt;
		}
		break;
	case VT_EMPTY:
	case VT_NULL:
		break;
	default:
		return false;
	}
	return true;
}

bool CADORecordset::IsFieldNull(LPCTSTR lpFieldName)
{
	_variant_t vtFld;
	
	vtFld = m_pRecordset->Fields->GetItem(lpFieldName)->Value;
	return vtFld.vt == VT_NULL;
}

bool CADORecordset::IsFieldNull(int nIndex)
{
	_variant_t vtFld;
	_variant_t vtIndex;

	vtIndex.vt = VT_I2;
	vtIndex.iVal = nIndex;
	
	vtFld = m_pRecordset->Fields->GetItem(vtIndex)->Value;
	return vtFld.vt == VT_NULL;
}

bool CADORecordset::IsFieldEmpty(LPCTSTR lpFieldName)
{
	_variant_t vtFld;
	
	vtFld = m_pRecordset->Fields->GetItem(lpFieldName)->Value;
	return vtFld.vt == VT_EMPTY || vtFld.vt == VT_NULL;
}

bool CADORecordset::IsFieldEmpty(int nIndex)
{
	_variant_t vtFld;
	_variant_t vtIndex;
	
	vtIndex.vt = VT_I2;
	vtIndex.iVal = nIndex;
		
	vtFld = m_pRecordset->Fields->GetItem(vtIndex)->Value;
	return vtFld.vt == VT_EMPTY || vtFld.vt == VT_NULL;
}

DWORD CADORecordset::GetRecordCount()
{
	DWORD nRows = 0;
	
	nRows = m_pRecordset->GetRecordCount();

	if(nRows == -1)
	{
		nRows = 0;
		if(m_pRecordset->EndOfFile != VARIANT_TRUE)
			m_pRecordset->MoveFirst();
		
		while(m_pRecordset->EndOfFile != VARIANT_TRUE)
		{
			nRows++;
			m_pRecordset->MoveNext();
		}
		if(nRows > 0)
			m_pRecordset->MoveFirst();
	}
	
	return nRows;
}

bool CADORecordset::IsOpen()
{
	if(m_pRecordset)
		return m_pRecordset->GetState() != adStateClosed;
	return false;
}

void CADORecordset::Close()
{
	try
	{
		if(IsOpen())
			m_pRecordset->Close();
	}
	
	catch(_com_error &e)
	{
		dump_com_error(e);
	}
		
}


bool CADODatabase::Execute(LPCTSTR lpstrExec)
{
    if (m_pConnection == NULL)

⌨️ 快捷键说明

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