📄 ado.cpp
字号:
// Ado.cpp: implementation of the CAdo class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "indent.h"
#include "Ado.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CAdo::CAdo()
{
::CoInitialize(NULL);
}
CAdo::~CAdo()
{
::CoUninitialize();
}
BOOL CAdo::Initialize()
{
try
{
m_pCon.CreateInstance(__uuidof(Connection));
m_pRts.CreateInstance(__uuidof(Recordset));
}
catch(_com_error e)
{
GetErrorMsg(e);
return FALSE;
}
return TRUE;
}
BOOL CAdo::UnInitialize()
{
try
{
m_pRts.Release();
m_pCon.Release();
}
catch(_com_error e)
{
GetErrorMsg(e);
return FALSE;
}
return TRUE;
}
BOOL CAdo::Close()
{
try
{
if(m_pRts->State == adStateOpen)
m_pRts->Close();
if(m_pCon->State == adStateOpen)
m_pCon->Close();
}
catch(_com_error e)
{
GetErrorMsg(e);
return FALSE;
}
return TRUE;
}
BOOL CAdo::Open(LPCTSTR lpConn)
{
try
{
if(m_pCon->State == adStateOpen)
m_pCon->Close();
m_pCon->ConnectionString = _bstr_t(lpConn);
m_pCon->Open("","","",adModeUnknown);
}
catch(_com_error e)
{
GetErrorMsg(e);
return FALSE;
}
return TRUE;
}
BOOL CAdo::Execute(LPCTSTR lpCmd)
{
try
{
if(m_pRts->State == adStateOpen)
m_pRts->Close();
m_pRts->Open(_variant_t(lpCmd),/*_variant_t((IDispatch*)m_pCon)*/
m_pCon->ConnectionString, adOpenDynamic,
adLockOptimistic, adCmdText);
}
catch(_com_error e)
{
GetErrorMsg(e);
return FALSE;
}
return TRUE;
}
void CAdo::GetErrorMsg(_com_error e)
{
CString strErrMsg;
strErrMsg.Format("连接数据库失败!\n\n错误代码:%s\n错误描述:%s",
e.ErrorMessage(),
(char*)e.Description());
AfxMessageBox(strErrMsg,MB_OK|MB_ICONERROR);
}
BOOL CAdo::IsEof()
{
return m_pRts->adoEOF;
}
CString CAdo::GetFieldsValue(LPCTSTR lpFields)
{
VARIANT var;
try
{
var = m_pRts->Fields->Item[lpFields]->GetValue();
if(var.vt != VT_NULL)
return (LPCTSTR)(_bstr_t)m_pRts->GetCollect(lpFields);
}
catch(_com_error e)
{
GetErrorMsg(e);
}
return _T("");
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -