?? adorecordset.cpp
字號:
// AdoRecordSet.cpp: implementation of the CAdoRecordset class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "AdoDatabase.h"
#include "AdoRecordSet.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CAdoRecordset::CAdoRecordset(CAdoDatabase* pDB)
{
m_pCmdChange=NULL;
m_pRecordset = NULL;
m_pDatabase = pDB;
m_nEdit = 0;
m_bOpen = FALSE;
m_nCount = 0;
m_pRecordset.CreateInstance(__uuidof(Recordset));
m_pCmdChange.CreateInstance(__uuidof(Command));
m_pCmdChange->ActiveConnection = m_pDatabase->m_pConnect;
}
CAdoRecordset::CAdoRecordset()
{
m_pCmdChange=NULL;
m_pRecordset = NULL;
m_pDatabase = NULL;
m_nEdit = 0;
m_bOpen = FALSE;
m_nCount = 0;
}
CAdoRecordset::~CAdoRecordset()
{
if(m_pRecordset)
{
if(m_nEdit != 1)
m_pRecordset->Close();
m_pRecordset.Release();
}
m_pDatabase = NULL;
}
BOOL CAdoRecordset::Open(CString strSQL)
{
HRESULT hr = NOERROR;
try
{
m_pRecordset.CreateInstance(__uuidof(Recordset));
m_pCmdChange.CreateInstance(__uuidof(Command));
m_pCmdChange->ActiveConnection = m_pDatabase->m_pConnect;
hr = m_pRecordset->Open(_variant_t(strSQL), m_pDatabase->m_pConnect.GetInterfacePtr(),adOpenDynamic,adLockOptimistic,adCmdText);
if (FAILED(hr))
{
m_bOpen = FALSE;
return FALSE;
}
m_nCount = 0;
m_bOpen = TRUE;
if(IsEOF())
return TRUE;
MoveFirst();
while(!IsEOF())
{
m_nCount++;
MoveNext();
}
MoveFirst();
}
catch (_com_error e)
{
m_bOpen = FALSE;
CString str = (char*)e.ErrorMessage();
return FALSE;
}
return TRUE;
}
BOOL CAdoRecordset::IsEOF()
{
int n = -1;
try
{
n = m_pRecordset->adoEOF;
}
catch(_com_error e)
{
m_nEdit = 0;
CString tc = (char*)e.ErrorMessage();
}
return n != 0;
}
BOOL CAdoRecordset::IsBOF()
{
int n = -1;
try
{
m_pRecordset->adoBOF;
}
catch(_com_error e)
{
m_nEdit = 0;
CString tc = (char*)e.ErrorMessage();
}
return n == 0;
}
void CAdoRecordset::MoveFirst()
{
try
{
m_pRecordset->MoveFirst();
}
catch(_com_error e)
{
m_nEdit = 0;
CString tc = (char*)e.ErrorMessage();
}
}
void CAdoRecordset::MoveNext()
{
try
{
m_pRecordset->MoveNext();
}
catch(_com_error e)
{
m_nEdit = 0;
CString tc = (char*)e.ErrorMessage();
}
}
void CAdoRecordset::MoveLast()
{
try
{
m_pRecordset->MoveLast();
}
catch(_com_error e)
{
m_nEdit = 0;
CString tc = (char*)e.ErrorMessage();
}
}
void CAdoRecordset::MovePrev()
{
try
{
m_pRecordset->MovePrevious();
}
catch(_com_error e)
{
m_nEdit = 0;
CString tc = (char*)e.ErrorMessage();
}
}
long CAdoRecordset::GetRecordCount()
{
// long l = m_pRecordset->GetRecordCount();
return m_nCount;
}
void CAdoRecordset::GetFieldValue(int nIndex, CString& strValue)
{
try
{
_variant_t var = m_pRecordset->GetCollect((long)nIndex);
if(var.vt != VT_NULL)
strValue = (LPCSTR)_bstr_t(var);
}
catch(_com_error e)
{
CString tc = (char*)e.ErrorMessage();
}
}
void CAdoRecordset::Close()
{
if(m_pRecordset)
{
if(m_nEdit != 1)
m_pRecordset->Close();
m_pRecordset.Release();
}
m_pDatabase = NULL;
m_bOpen = FALSE;
}
void CAdoRecordset::GetFieldValue(CString strName, CString &strValue)
{
try
{
_variant_t var = m_pRecordset->GetCollect((_variant_t)strName);
if(var.vt != VT_NULL)
strValue = (LPCSTR)_bstr_t(var);
}
catch(_com_error e)
{
CString tc = (char*)e.ErrorMessage();
}
}
void CAdoRecordset::AddNew()
{
try
{
m_nEdit = 1;
m_pRecordset->AddNew();
}
catch(_com_error e)
{
m_nEdit = 0;
CString tc = (char*)e.ErrorMessage();
}
}
void CAdoRecordset::Edit()
{
m_nEdit = 1;
}
BOOL CAdoRecordset::Update()
{
HRESULT hr = 0;
try
{
if(m_nEdit == 1)
{
m_nEdit = 2;
hr = m_pRecordset->Update();
}
}
catch(_com_error e)
{
CString tc = (char*)e.ErrorMessage();
}
return hr == 0;
}
void CAdoRecordset::SetFieldValue(int nIndex, CString strValue)
{
try
{
m_pRecordset->PutCollect(long(nIndex), _variant_t(strValue));
}
catch(_com_error e)
{
CString tc = (char*)e.ErrorMessage();
}
}
void CAdoRecordset::SetFieldValue(CString strName, CString strValue)
{
try
{
m_pRecordset->PutCollect((_variant_t)strName, _variant_t(strValue));
}
catch(_com_error e)
{
CString tc = (char*)e.ErrorMessage();
}
}
BOOL CAdoRecordset::Delete()
{
HRESULT hr = 0;
try
{
m_pRecordset->Delete(adAffectCurrent);
hr = m_pRecordset->Update();
}
catch(_com_error e)
{
hr = 1;
CString tc = (char*)e.ErrorMessage();
}
return hr == 0;
}
BOOL CAdoRecordset::IsOpen()
{
// long l = m_pRecordset->State;
return FALSE;
}
long CAdoRecordset::GetFieldCount()
{
FieldsPtr pts = m_pRecordset->GetFields();
return pts->GetCount();
}
BOOL CAdoRecordset::DeleteAll()
{
try
{
m_pRecordset->Delete(adAffectAll);
m_pRecordset->Update();
}
catch(_com_error e)
{
CString tc = (char*)e.ErrorMessage();
return FALSE;
}
return TRUE;
}
BOOL CAdoRecordset::GetQuery(CString strSQL)
{
try
{
m_pCmdChange->CommandText = (_bstr_t)strSQL;
m_pRecordset = m_pCmdChange->Execute(NULL,NULL,adCmdText);
if(m_pRecordset == NULL)
return FALSE;
m_nCount = 0;
m_bOpen = TRUE;
if(IsEOF())
return TRUE;
MoveFirst();
while(!IsEOF())
{
m_nCount++;
MoveNext();
}
MoveFirst();
}
catch (_com_error e)
{
m_pCmdChange->Cancel();
AfxMessageBox(e.ErrorMessage());
return FALSE;
}
return TRUE;
}
BOOL CAdoRecordset::SetBlobValue(int nField, CString strPath)
{
DWORD nFileLen;
CFile file;
if( !file.Open(strPath, CFile::modeRead) )
return FALSE;
nFileLen = file.GetLength();
char* pBuf = new char[nFileLen + 1];
if(file.ReadHuge(pBuf,nFileLen) != nFileLen)
return FALSE;
VARIANT varBLOB;
SAFEARRAY *psa;
SAFEARRAYBOUND rgsabound[1];
HRESULT hr = NOERROR;
try
{
if(pBuf)
{
rgsabound[0].lLbound = 0;
rgsabound[0].cElements = nFileLen;
psa = SafeArrayCreate(VT_UI1, 1, rgsabound);
for (long i = 0; i < (long)nFileLen; i++)
SafeArrayPutElement (psa, &i, pBuf++);
varBLOB.vt = VT_ARRAY | VT_UI1;
varBLOB.parray = psa;
m_pRecordset->GetFields()->GetItem((long)nField)->AppendChunk(varBLOB);
}
}
catch (_com_error e)
{
AfxMessageBox(e.ErrorMessage());
e.ErrorInfo();
return FALSE;
}
return TRUE;
}
BOOL CAdoRecordset::SetBlobValue(CString strField, CString strPath)
{
DWORD nFileLen;
CFile file;
if( !file.Open(strPath, CFile::modeRead) )
return FALSE;
nFileLen = file.GetLength();
char* pBuf = new char[nFileLen + 1];
if(file.ReadHuge(pBuf,nFileLen) != nFileLen)
return FALSE;
VARIANT varBLOB;
SAFEARRAY *psa;
SAFEARRAYBOUND rgsabound[1];
HRESULT hr = NOERROR;
try
{
if(pBuf)
{
rgsabound[0].lLbound = 0;
rgsabound[0].cElements = nFileLen;
psa = SafeArrayCreate(VT_UI1, 1, rgsabound);
for (long i = 0; i < (long)nFileLen; i++)
SafeArrayPutElement (psa, &i, pBuf++);
varBLOB.vt = VT_ARRAY | VT_UI1;
varBLOB.parray = psa;
m_pRecordset->GetFields()->GetItem(strField.operator LPCTSTR())->AppendChunk(varBLOB);
}
}
catch (_com_error e)
{
AfxMessageBox(e.ErrorMessage());
e.ErrorInfo();
return FALSE;
}
return TRUE;
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -