📄 adoconn.cpp
字号:
// ADOConn.cpp: implementation of the ADOConn class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "FastSellStore.h"
#include "ADOConn.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
ADOConn::ADOConn()
{
}
ADOConn::~ADOConn()
{
}
void ADOConn::OnInitDBConnect()
{
/* Initilization COM Componment */
::CoInitialize(NULL);
}
_RecordsetPtr &ADOConn::GetRecordSet (_bstr_t bstrSQL)
{
try
{
// connect database, IF Connection is NULL, THEN Resume
if( m_pConnection == NULL)
this->OnInitDBConnect();
// Create Recordset Object
m_pRecordset.CreateInstance (__uuidof(Recordset));
// m_pRecordset->CursorLocation = adUseClient;
// Get Record in TABLE
m_pRecordset->Open (bstrSQL, m_pConnection.GetInterfacePtr(),adOpenDynamic,
adLockOptimistic, adCmdText);
}
// catch UNNormal
catch( _com_error e)
{
// Display error INFORMATION
AfxMessageBox (e.Description());
}
// Return Recordset
return this->m_pRecordset;
}
bool ADOConn::ExecuteSQL (_bstr_t bstrSQL)
{
//::_variant_t RecordsetAffected;
try
{
// whether connect to database
if(this->m_pConnection == NULL)
this->OnInitDBConnect(); // resume
// Execute
this->m_pConnection->Execute (bstrSQL, NULL, adCmdText);
// return true
return true;
}
catch (_com_error e)
{
AfxMessageBox (e.Description());
return false;
}
}
void ADOConn::ExitConnect()
{
// Close Recordset and Connection
if(this->m_pRecordset != NULL)
this->m_pRecordset->Close();
this->m_pConnection->Close();
this->m_pConnection = NULL;
this->m_pRecordset = NULL;
// release ENU
::CoUninitialize();
}
bool ADOConn::MovePrevious ()
{
try
{
this->m_pRecordset->MovePrevious ();
}
catch (_com_error e)
{
AfxMessageBox (e.Description () );
return false;
}
return true;
}
bool ADOConn::MoveLast ()
{
try
{
this->m_pRecordset->MoveLast ();
}
catch (_com_error e)
{
AfxMessageBox (e.Description () );
return false;
}
return true;
}
bool ADOConn::MoveNext ()
{
try
{
this->m_pRecordset->MoveNext();
}
catch (_com_error e)
{
AfxMessageBox (e.Description () );
return false;
}
return true;
}
bool ADOConn::MoveFirst ()
{
int ncount = this->m_pRecordset->GetRecordCount();
try
{ //|
// if(!this->m_pRecordset->adoEOF||!this->m_pRecordset->adoBOF)
this->m_pRecordset->MoveFirst ();
}
catch (_com_error e)
{
AfxMessageBox (e.Description () );
return false;
}
return true;
}
long ADOConn::GetRecordCount ()
{
long nCount = 0;
try
{
this->m_pRecordset->MoveFirst ();
}
catch (...)
{
return 0;
}
// Check range
if(this->m_pRecordset->adoEOF)
return 0;
// Count the number
while (!this->m_pRecordset->adoEOF)
{
m_pRecordset->MoveNext ();
nCount = nCount + 1;
}
// return first pointer
this->m_pRecordset->MoveFirst ();
return nCount;
}
bool ADOConn::Move (int nRecordNum)
{
try
{
// check range
if(!this->m_pRecordset->adoBOF)
this->m_pRecordset->MoveFirst ();
this->m_pRecordset->Move (nRecordNum);
}
catch (_com_error e)
{
AfxMessageBox (e.Description () );
return false;
}
return true;
}
CString ADOConn::GetFieldValue (CString Field)
{
_variant_t TheValue;
CString temp;
// Get the value specified colume
// and Set the value to TheValue
TheValue = this->m_pRecordset->GetCollect((_bstr_t)Field);
if(TheValue.vt == VT_EMPTY || TheValue.vt == VT_NULL)
temp = "";
else
{
temp = (char*)(_bstr_t)TheValue;
temp.TrimRight();
temp.TrimLeft();
}
return temp;
}
void ADOConn::GetErrors (_com_error eErrors)
{
ErrorsPtr pErrors = this->m_pConnection->GetErrors();
if(pErrors->GetCount == 0)
MessageBox(NULL,eErrors.ErrorMessage(),"错 误",MB_OK|MB_ICONEXCLAMATION);
else
{
for(int i = 0; i < pErrors->GetCount(); ++i)
{
_bstr_t desc = pErrors->GetItem((long)i)->GetDescription();
MessageBox(NULL,desc,"错 误",MB_OK|MB_ICONEXCLAMATION);
}
}
}
int ADOConn::GetFieldCount()
{
int count = 0;
try
{
count = m_pRecordset->GetFields()->GetCount();
}
catch(...)
{
return -1;
}
return count;
}
CString ADOConn::GetFieldName(int nNumber)
{
CString sName;
_variant_t vName;
try
{
vName = m_pRecordset->GetFields()->GetItem((long)nNumber)->GetName();
}
catch(_com_error &e)
{
GetErrors(e);
return "";
}
if(vName.vt == VT_EMPTY)
return "";
sName = (char*)(_bstr_t)vName;
sName.TrimLeft();
sName.TrimRight();
return sName;
}
CString ADOConn::GetFieldType(int nNumber)
{
ADODB::DataTypeEnum type;
CString sType;
try
{
m_pRecordset->GetFields()->GetItem((long)nNumber)->get_Type(&type);
}
catch(_com_error &e)
{
GetErrors(e);
return adError;
}
ADODB::DataTypeEnum tp[]={adBinary,adBoolean,adCurrency,adBigInt,adDecimal,adDouble,adInteger,adLongVarBinary,adNumeric,adSingle,adSmallInt,adTinyInt,adUnsignedBigInt,adUnsignedInt,adUnsignedSmallInt,adUnsignedTinyInt,adVarBinary,adBSTR,adChar,adLongVarChar,adLongVarWChar,adVarChar,adVarWChar,adWChar,adDate,adDBDate,adDBTime,adDBTimeStamp};
int i = 0;
for(i=0;i<28;i++)
{
if(type==tp[i])
break;
if(i==27)
return "未知类型";
}
if(i<2)
sType="逻辑型";
if(i>=2&& i<18)
sType="数值型";
if(i>=18 && i<25)
sType="字符型";
if(i>=25 && i<29)
sType="日期型";
return sType;
}
bool ADOConn::IsNull(int nIndex)
{
long Attrib = m_pRecordset->GetFields()->GetItem((long)nIndex)->Attributes;
if(Attrib & adFldIsNullable)
return false;
else
return true;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -