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

📄 ado.cpp

📁 一个自己修改过的ado类
💻 CPP
📖 第 1 页 / 共 2 页
字号:
    {
        CString strEvent;
        strEvent.Format("- Connection is NULL.\n");
        DelLogDebugEvent("CADODatabase", "Execute", strEvent, eDELIMPORTANCE_LEVEL_HIGHEST, __FILE__, __LINE__);
		return false;
    }
	
    if (strcmp(lpstrExec, _T("")) == 0)
    {
        CString strEvent;
        strEvent.Format("- Execute command is empty.\n");
        DelLogDebugEvent("CADODatabase", "Execute", strEvent, eDELIMPORTANCE_LEVEL_HIGHEST, __FILE__, __LINE__);
		return false;
    }
	
	try
	{
		m_pConnection->Execute(_bstr_t(lpstrExec), NULL, adExecuteNoRecords);
	}
	catch(_com_error &e)
	{
		dump_com_error(e);
	}
	return true;	
}

bool CADORecordset::RecordBinding(CADORecordBinding &pAdoRecordBinding)
{
	IADORecordBinding *picRs = NULL;
	HRESULT hr;

	//Open the binding interface.
	if(FAILED(hr = m_pRecordset->QueryInterface(__uuidof(IADORecordBinding), (LPVOID*)&picRs )))
	{
		_com_issue_error(hr);
		return false;
	}
	
	//Bind the recordset to class
	if(FAILED( hr = picRs->BindToRecordset(&pAdoRecordBinding)))
	{
		_com_issue_error(hr);
		return false;
	}
	return true;
}

void CADORecordset::dump_com_error(_com_error &e)
{
	CString ErrorStr;	
	
	_bstr_t bstrSource(e.Source());
	_bstr_t bstrDescription(e.Description());
	ErrorStr.Format( "CADORecordset 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("Query = " + GetQuery() + '\n' + ErrorStr);

//	#ifdef _DEBUG
//		AfxMessageBox( ErrorStr, MB_OK | MB_ICONERROR );
//	#endif	
    
    DelLogDebugEvent("CADORecordset", "dump_com_error", ErrorStr, eDELIMPORTANCE_LEVEL_HIGH, __FILE__, __LINE__);
}

bool CADORecordset::GetFieldInfo(LPCTSTR lpFieldName, CADOFieldInfo* fldInfo)
{
	_variant_t vtFld;
#if _MSC_VER >= 1300
	strcpy_s(fldInfo->m_strName,sizeof(fldInfo->m_strName), (LPCTSTR)m_pRecordset->Fields->GetItem(lpFieldName)->GetName());
#else
	strcpy(fldInfo->m_strName, (LPCTSTR)m_pRecordset->Fields->GetItem(lpFieldName)->GetName());
#endif
	

	fldInfo->m_lSize = m_pRecordset->Fields->GetItem(lpFieldName)->GetActualSize();
	fldInfo->m_lDefinedSize = m_pRecordset->Fields->GetItem(lpFieldName)->GetDefinedSize();
	fldInfo->m_nType = m_pRecordset->Fields->GetItem(lpFieldName)->GetType();
	fldInfo->m_lAttributes = m_pRecordset->Fields->GetItem(lpFieldName)->GetAttributes();
	return true;
}

bool CADORecordset::GetFieldInfo(int nIndex, CADOFieldInfo* fldInfo)
{
	_variant_t vtFld;
	_variant_t vtIndex;
	
	vtIndex.vt = VT_I2;
	vtIndex.iVal = nIndex;
		
#if _MSC_VER >= 1300
	strcpy_s(fldInfo->m_strName,sizeof(fldInfo->m_strName), (LPCTSTR)m_pRecordset->Fields->GetItem(vtIndex)->GetName());
#else
	strcpy(fldInfo->m_strName, (LPCTSTR)m_pRecordset->Fields->GetItem(vtIndex)->GetName());
#endif
	
	fldInfo->m_lSize = m_pRecordset->Fields->GetItem(vtIndex)->GetActualSize();
	fldInfo->m_lDefinedSize = m_pRecordset->Fields->GetItem(vtIndex)->GetDefinedSize();
	fldInfo->m_nType = m_pRecordset->Fields->GetItem(vtIndex)->GetType();
	fldInfo->m_lAttributes = m_pRecordset->Fields->GetItem(vtIndex)->GetAttributes();
	return true;
}


bool CADORecordset::GetChunk(LPCTSTR lpFieldName, CString& strValue)
{
	CString str = _T("");
	long lngSize, lngOffSet = 0;
	_variant_t varChunk;    
	int ChunkSize = 100;

	lngSize = m_pRecordset->Fields->GetItem(lpFieldName)->ActualSize;
	
	str.Empty();
	while(lngOffSet < lngSize)
	{
		varChunk = m_pRecordset->Fields->GetItem(lpFieldName)->GetChunk(ChunkSize);
		str += varChunk.bstrVal;
		lngOffSet += ChunkSize;
	}

	lngOffSet = 0;
	strValue = str;
	return TRUE;
}

CString CADORecordset::GetString(LPCTSTR lpCols, LPCTSTR lpRows, LPCTSTR lpNull, long numRows)
{
	_bstr_t varOutput;
	_bstr_t varNull("");
	_bstr_t varCols("\t");
	_bstr_t varRows("\r");

	if(strlen(lpCols) != 0)
		varCols = _bstr_t(lpCols);

	if(strlen(lpRows) != 0)
		varRows = _bstr_t(lpRows);
	
	if(numRows == 0)
		numRows =(long)GetRecordCount();			
			
	varOutput = m_pRecordset->GetString(adClipString, numRows, varCols, varRows, varNull);

	return (LPCTSTR)varOutput;
}

CString IntToStr(int nVal)
{
	CString strRet;
	strRet.Format("%d",nVal);
	return strRet;
}

CString LongToStr(long lVal)
{
	CString strRet;
	strRet.Format("%d",lVal);
	return strRet;
}

void CADORecordset::Edit()
{
	m_nEditStatus = dbEdit;
}

bool CADORecordset::AddNew()
{
	m_nEditStatus = dbEditNone;
	if(m_pRecordset->AddNew() != S_OK)
		return false;

	m_nEditStatus = dbEditNew;
	return true;
}

bool CADORecordset::Update()
{
	bool bret = true;

	if(m_nEditStatus != dbEditNone)
	{
		if(m_pRecordset->Update() != S_OK)
			bret = false;
	}

	m_nEditStatus = dbEditNone;
	return bret;
}

void CADORecordset::CancelUpdate()
{
	m_pRecordset->CancelUpdate();
	m_nEditStatus = dbEditNone;
}

bool CADORecordset::SetFieldValue(int nIndex, CString strValue)
{
	if(strValue.IsEmpty()) return true;
	
	_variant_t vtFld;
    vtFld.vt = VT_BSTR;
    vtFld.bstrVal = _bstr_t(strValue);

	_variant_t vtIndex;	
	vtIndex.vt = VT_I2;
	vtIndex.iVal = nIndex;
	
	m_pRecordset->Fields->GetItem(vtIndex)->Value = _bstr_t(vtFld);//_bstr_t(strValue);
	return true;

}

bool CADORecordset::SetFieldValue(LPCTSTR lpFieldName, CString strValue)
{
	if(strValue.IsEmpty()) return true;
	
	_variant_t vtFld;
    vtFld.vt = VT_BSTR;
    vtFld.bstrVal = _bstr_t(strValue);
	
	m_pRecordset->Fields->GetItem(lpFieldName)->Value = _bstr_t(vtFld);//_bstr_t(strValue);
	return true;
	
}

bool CADORecordset::SetFieldValue(int nIndex, int nValue)
{
	_variant_t vtFld;
	vtFld.vt = VT_I2;
	vtFld.iVal = nValue;
	
	_variant_t vtIndex;
	
	vtIndex.vt = VT_I2;
	vtIndex.iVal = nIndex;
	
	m_pRecordset->Fields->GetItem(vtIndex)->Value = vtFld;
	return true;
	
}

bool CADORecordset::SetFieldValue(LPCTSTR lpFieldName, int nValue)
{
	_variant_t vtFld;
	vtFld.vt = VT_I2;
	vtFld.iVal = nValue;
	
	m_pRecordset->Fields->GetItem(lpFieldName)->Value = vtFld;
	return true;
	
}

bool CADORecordset::SetFieldValue(int nIndex, long lValue)
{
	_variant_t vtFld;
	vtFld.vt = VT_I4;
	vtFld.lVal = lValue;
	
	_variant_t vtIndex;
	
	vtIndex.vt = VT_I2;
	vtIndex.iVal = nIndex;
	
	m_pRecordset->Fields->GetItem(vtIndex)->Value = vtFld;
	return true;
	
}

bool CADORecordset::SetFieldValue(LPCTSTR lpFieldName, long lValue)
{
	_variant_t vtFld;
	vtFld.vt = VT_I4;
	vtFld.lVal = lValue;
	
	m_pRecordset->Fields->GetItem(lpFieldName)->Value = vtFld;
	return true;
	
}

bool CADORecordset::SetFieldValue(int nIndex, double dblValue)
{
	_variant_t vtFld;
	vtFld.vt = VT_R8;
	vtFld.dblVal = dblValue;

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

bool CADORecordset::SetFieldValue(LPCTSTR lpFieldName, double dblValue)
{
	_variant_t vtFld;
	vtFld.vt = VT_R8;
	vtFld.dblVal = dblValue;
		
	m_pRecordset->Fields->GetItem(lpFieldName)->Value = vtFld;
	return true;
	
}

bool CADORecordset::SetFieldValue(int nIndex, COleDateTime time)
{
	_variant_t vtFld;
	vtFld.vt = VT_DATE;
	vtFld.date = time;
	
	_variant_t vtIndex;
	
	vtIndex.vt = VT_I2;
	vtIndex.iVal = nIndex;
	
	m_pRecordset->Fields->GetItem(vtIndex)->Value = vtFld;
	return true;
	
}

bool CADORecordset::SetFieldValue(LPCTSTR lpFieldName, COleDateTime time)
{
	_variant_t vtFld;
	vtFld.vt = VT_DATE;
	vtFld.date = time;
	
	m_pRecordset->Fields->GetItem(lpFieldName)->Value = vtFld;
	return true;
	
}

bool CADORecordset::SetFieldValue(LPCTSTR lpFieldName, BYTE *pBlob, UINT uLen)
{
	_variant_t		varBLOB;
	SAFEARRAY		*psa;
	SAFEARRAYBOUND	rgsabound[1];
					
	if(pBlob)
	{    
		rgsabound[0].lLbound = 0;
		rgsabound[0].cElements = uLen;
		psa = SafeArrayCreate(VT_UI1, 1, rgsabound);
		for (long i = 0; i < (long)uLen; i++)
			SafeArrayPutElement (psa, &i, pBlob++);
		varBLOB.vt = VT_ARRAY | VT_UI1;
		varBLOB.parray = psa;

		m_pRecordset->GetFields()->GetItem(lpFieldName)->AppendChunk(varBLOB);	
	}

	return true;
}

bool CADORecordset::SetBookmark()
{
	if(m_varBookmark.vt != VT_EMPTY)
	{
		m_pRecordset->Bookmark = m_varBookmark;
		return true;
	}
	return false;
}

bool CADORecordset::Delete()
{
	if(m_pRecordset->Delete(adAffectCurrent) != S_OK)
		return false;

	if(m_pRecordset->Update() != S_OK)
		return false;

	return true;
}

bool CADORecordset::Find(LPCTSTR lpFind, int nSearchDirection)
{

	m_strFind = lpFind;
	m_nSearchDirection = nSearchDirection;

    if (m_strFind.IsEmpty())
    {
        CString strEvent;
        strEvent.Format("- Search string is empty.\n");
        DelLogDebugEvent("CADORecordset", "Find", strEvent, eDELIMPORTANCE_LEVEL_HIGHEST, __FILE__, __LINE__);
		return false;
    }
	
	if(m_nSearchDirection == searchForward)
	{
		m_pRecordset->Find(_bstr_t(m_strFind), 0, adSearchForward, "");
		if(!IsEof())
		{
			m_varBookFind = m_pRecordset->Bookmark;
			return true;
		}
	}
	else if(m_nSearchDirection == searchBackward)
	{
		m_pRecordset->Find(_bstr_t(m_strFind), 0, adSearchBackward, "");
		if(!IsBof())
		{
			m_varBookFind = m_pRecordset->Bookmark;
			return true;
		}
	}
	else
	{
        CString strEvent;
        strEvent.Format("- Invalid search direction (%d).\n", nSearchDirection);
        DelLogDebugEvent("CADORecordset", "Find", strEvent, eDELIMPORTANCE_LEVEL_HIGHEST, __FILE__, __LINE__);
		m_nSearchDirection = searchForward;
	}
	return false;
}

bool CADORecordset::FindFirst(LPCTSTR lpFind)
{
	m_pRecordset->MoveFirst();
	return Find(lpFind);
}

bool CADORecordset::FindNext()
{
	if(m_nSearchDirection == searchForward)
	{
		m_pRecordset->Find(_bstr_t(m_strFind), 1, adSearchForward, m_varBookFind);
		if(!IsEof())
		{
			m_varBookFind = m_pRecordset->Bookmark;
			return true;
		}
	}
	else
	{
		m_pRecordset->Find(_bstr_t(m_strFind), 1, adSearchBackward, m_varBookFind);
		if(!IsBof())
		{
			m_varBookFind = m_pRecordset->Bookmark;
			return true;
		}
	}
	return false;
}

bool CADORecordset::Supports(int nCursorOption)
{
    if(TRUE == m_pRecordset->Supports((CursorOptionEnum)nCursorOption))
        return true;

    return false;
}

bool CADORecordset::CanAppend()
{
    if (TRUE == m_pRecordset->Supports(adAddNew))
        return true;

    return false;
}

bool CADORecordset::CanBookmark()
{
    if (TRUE == m_pRecordset->Supports(adBookmark))
        return true;

    return false;
}

bool CADORecordset::CanResync()
{
    if (TRUE == m_pRecordset->Supports(adResync))
        return true;

    return false;
}

bool CADORecordset::CanUpdate()
{
    if (TRUE == m_pRecordset->Supports(adUpdate))
        return true;

    return false;
}

bool CADORecordset::Requery()
{
    HRESULT hr = S_OK;

    hr = m_pRecordset->Requery(adCmdUnknown);

    return (hr == S_OK);
}

bool CADORecordset::Resync()
{
	HRESULT hr = S_OK;

    hr = m_pRecordset->Resync(adAffectAll, adResyncAllValues);

    return (hr == S_OK);
}

int CADORecordset::GetCursorLocation()
{
    return (enum cadoCursorLocationEnum)m_pRecordset->GetCursorLocation();
}

void CADORecordset::SetCursorLocation(int nCursorLocation)
{
    m_nCursorLocation = (enum cadoCursorLocationEnum)nCursorLocation;
}

int CADORecordset::GetCursorType()
{
    return (enum cadoCursorTypeEnum)m_pRecordset->GetCursorType();
}

void CADORecordset::SetCursorType(int nCursorType)
{
    m_nCursorType = (enum cadoCursorTypeEnum)nCursorType;
}

int CADORecordset::GetLockType()
{
    return (enum cadoLockTypeEnum)m_pRecordset->GetLockType();
}

void CADORecordset::SetLockType(int nLockType)
{
    m_nLockType = (enum cadoLockTypeEnum)nLockType;
}

void CADORecordset::SetFilter(CString strFilter)
{
    m_pRecordset->Filter = (LPCSTR)strFilter;
}

void CADORecordset::SetFilter(int nFilterType)
{
	_variant_t vtFilterType;	
	vtFilterType.vt = VT_I2;
    vtFilterType.iVal = nFilterType;

    m_pRecordset->Filter = vtFilterType;
}

void CADORecordset::SetSortOrder(CString strSort)
{
    m_pRecordset->Sort = (LPCSTR)strSort;
}

bool CADORecordset::NextRecordset()
{
    m_pRecordset = m_pRecordset->NextRecordset(NULL);
    if (m_pRecordset) return true;
    return false;
}

⌨️ 快捷键说明

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