📄 hadodatabase.cpp
字号:
// HADODatabase.cpp: implementation of the CADODatabase class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "HADODatabase.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CADODatabase::CADODatabase()
{
m_bOpened=FALSE;
}
CADODatabase::~CADODatabase()
{
}
//打开一个数据库:
BOOL CADODatabase::Open(LPCTSTR lpszName)
{
m_bOpened=FALSE;
try
{
//打开数据库:
m_pCon.CreateInstance(_uuidof(Connection));
m_pCon->Open(_bstr_t(lpszName),_bstr_t(L""),_bstr_t(L""),adModeUnknown);
m_bOpened=TRUE;
}
catch(_com_error &e)
{
CString str;
str=e.ErrorMessage();
str="1 "+str;
AfxMessageBox(str);
}
return m_bOpened;
}
//关闭一个数据库:
BOOL CADODatabase::Close()
{
if(m_bOpened)
{
m_bOpened=FALSE;
m_pCon->Close();
}
return TRUE;
}
//判断数据库是否打开:
BOOL CADODatabase::IsOpen()
{
return m_bOpened;
}
//执行SQL查询:
BOOL CADODatabase::Execute(LPCTSTR lpszSQL)
{
//执行SQL查询:
m_SQL=lpszSQL;
m_SQL.MakeUpper();
try
{
_bstr_t bstrSQL(lpszSQL);
_variant_t vRa(0L);
m_pCon->Execute(bstrSQL,&vRa,adOptionUnspecified);
return TRUE;
}
catch(_com_error &e)
{
CString str;
str=e.ErrorMessage();
str="2 "+str;
AfxMessageBox(str);
return FALSE;
}
}
//事务处理:
BOOL CADODatabase::BeginTrans()
{
if(!m_bOpened)
{
AfxMessageBox("应该先打开数据库!");
return FALSE;
}
try
{
m_pCon->BeginTrans();
}
catch(_com_error &e)
{
CString str;
str=e.ErrorMessage();
str="3 "+str;
AfxMessageBox(str);
return FALSE;
}
return TRUE;///SXX
}
//提交事务:
BOOL CADODatabase::CommitTrans()
{
if(!m_bOpened)
{
AfxMessageBox("应该先打开数据库!");
return FALSE;
}
try
{
m_pCon->CommitTrans();
}
catch(_com_error &e)
{
CString str;
str=e.ErrorMessage();
str="4 "+str;
AfxMessageBox(str);
return FALSE;
}
return TRUE;///SXX
}
//回滚事务:
BOOL CADODatabase::RollBackTrans()
{
if(!m_bOpened)
{
AfxMessageBox("应该先打开数据库!");
return FALSE;
}
try
{
m_pCon->RollbackTrans();
}
catch(_com_error &e)
{
CString str;
str=e.ErrorMessage();
str="5 "+str;
AfxMessageBox(str);
return FALSE;
}
return TRUE;///SXX
}
//////////////////////////////////////////////////////////////////////
// CADORecordset Class
//////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CADORecordset::CADORecordset()
{
m_Opened=FALSE;
}
CADORecordset::~CADORecordset()
{
}
//与打开、关闭记录集相关的函数:
BOOL CADORecordset::Open(LPCTSTR lpszSQL)
{
//如果数据库没有打开,则返回:
if(!m_pDatabase->IsOpen())
{
AfxMessageBox("必须先打开数据库!");
return FALSE;
}
//只能执行SELECT语句:
m_pDatabase->m_SQL=lpszSQL;
m_pDatabase->m_SQL.MakeUpper();
if(m_pDatabase->m_SQL.Find("SELECT")==-1)
{
AfxMessageBox("在Open函数中只能执行SELECT 语句!");
return FALSE;
}
try
{
//执行SQL语句,获取记录集:
_bstr_t bstrSQL(lpszSQL);
_variant_t vRa(0L);
m_set=m_pDatabase->m_pCon->Execute(bstrSQL,&vRa,adOptionUnspecified);
m_Opened=TRUE;
return TRUE;
}
catch(_com_error &e)
{
CString str;
str=e.ErrorMessage();
str="6 "+str;
AfxMessageBox(str);
return FALSE;
}
}
BOOL CADORecordset::Close()
{
try
{
m_set->Close();
m_Opened=FALSE;
return TRUE;
}
catch(_com_error &e)
{
CString str;
str=e.ErrorMessage();
str="7 "+str;
AfxMessageBox(str);
return FALSE;
}
}
BOOL CADORecordset::IsOpen()
{
return m_Opened;
}
//与设置、获取记录集相关的参数:
CString CADORecordset::GetFieldName(long index)
{
if(!IsOpen())
{
AfxMessageBox("应该先打开记录集!");
return "";
}
if(GetFieldCount()<index)
{
AfxMessageBox("字段序号越界!");
return "";
}
CString re;
try
{
Field * field=NULL;
VARIANT ind;
ind.vt=VT_I4;
ind.lVal=index;
//获取Field对象:
m_set->Fields->get_Item(ind,&field);
//获取字段名:
if(field)
{
//获取字段值:
_variant_t var;
var=field->GetName();
re=var.bstrVal;
}
else
{
AfxMessageBox("读取Field出错!");
re="***";
}
return re;
}
catch(_com_error &e)
{
CString str;
str=e.ErrorMessage();
str="8 "+str;
AfxMessageBox(str);
return "";
}
}
long CADORecordset::GetFieldCount()
{
return m_set->Fields->Count;
}
_variant_t CADORecordset::GetFieldValue(long index)
{
_variant_t re;
if(!IsOpen())
{
AfxMessageBox("应该先打开记录集!");
re.vt=VT_NULL;
return re;
}
if(GetFieldCount()<index)
{
AfxMessageBox("字段序号越界!");
re.vt=VT_NULL;
return re;
}
try
{
Field * field=NULL;
VARIANT ind;
ind.vt=VT_I4;
ind.lVal=index;
//获取字段值:
re=m_set->GetCollect(ind);
return re;
}
catch(_com_error &e)
{
CString str;
str=e.ErrorMessage();
str="9 "+str;
AfxMessageBox(str);
return "";
}
}
bool CADORecordset::GetFieldValue(LPCTSTR lpFieldName, CString& strValue)
{
CString str = _T("");
_variant_t vtFld;
CString strRet;
char buff[17];
vtFld = m_set->Fields->GetItem(lpFieldName)->Value;
switch(vtFld.vt)
{
case VT_BSTR:
str = vtFld.bstrVal;
break;
case VT_I4:
itoa(vtFld.iVal, buff, 17);
str = buff;
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::SetFieldValue(long index,_variant_t var)
{
/* if(!IsOpen())
{
AfxMessageBox("应该先打开记录集!");
return FALSE;
}
if(GetFieldCount()<index)
{
AfxMessageBox("字段序号越界!");
return FALSE;
}
Field * field;
VARIANT ind;
try
{
ind.vt=VT_I4;
ind.lVal=index;
//获取Field对象:
if(field)
{
//获取字段值:
m_set->Fields->get_Item(ind,&field);
//设置字段值:
field->Value=var;
}
else
{
AfxMessageBox("读取Field出错!");
return FALSE;
}
return TRUE;
}
catch(_com_error &e)
{
CString str;
str=e.ErrorMessage();
str="10 "+str;
AfxMessageBox(str);
return FALSE;
}
*/
return TRUE; //SXX
}
BOOL CADORecordset::Update()
{
/* if(!IsOpen())
{
AfxMessageBox("应该先打开记录集!");
return FALSE;
}
try
{
m_set->Update();
return TRUE;
}
catch(_com_error &e)
{
CString str;
str=e.ErrorMessage();
str="11 "+str;
AfxMessageBox(str);
return FALSE;
}
*/
return TRUE; //SXX
}
BOOL CADORecordset::CancelUpdate()
{
if(!IsOpen())
{
AfxMessageBox("应该先打开记录集!");
return FALSE;
}
try
{
m_set->CancelUpdate();
return TRUE;
}
catch(_com_error &e)
{
CString str;
str=e.ErrorMessage();
str="12 "+str;
AfxMessageBox(str);
return FALSE;
}
}
BOOL CADORecordset::ReQuery()
{
if(!IsOpen())
{
AfxMessageBox("应该先打开记录集!");
return FALSE;
}
try
{
m_set->Requery(6);
return TRUE;
}
catch(_com_error &e)
{
CString str;
str=e.ErrorMessage();
str="13 "+str;
AfxMessageBox(str);
return FALSE;
}
}
//与在记录集中移动相关的函数:
BOOL CADORecordset::IsEOF()
{
return m_set->GetISEOF();
}
BOOL CADORecordset::IsBOF()
{
return m_set->GetBOF();
}
BOOL CADORecordset::MoveFirst()
{
if(!IsOpen())
{
AfxMessageBox("应该先打开记录集!");
return FALSE;
}
try
{
m_set->MoveFirst();
return TRUE;
}
catch(_com_error &e)
{
CString str;
str=e.ErrorMessage();
str="14 "+str;
AfxMessageBox(str);
return FALSE;
}
}
BOOL CADORecordset::Move(long disp)
{
if(!IsOpen())
{
AfxMessageBox("应该先打开记录集!");
return FALSE;
}
try
{
m_set->Move(disp);
return TRUE;
}
catch(_com_error &e)
{
CString str;
str=e.ErrorMessage();
str="15 "+str;
AfxMessageBox(str);
return FALSE;
}
}
BOOL CADORecordset::MoveLast()
{
if(!IsOpen())
{
AfxMessageBox("应该先打开记录集!");
return FALSE;
}
try
{
m_set->MoveLast();
return TRUE;
}
catch(_com_error &e)
{
CString str;
str=e.ErrorMessage();
str="16 "+str;
AfxMessageBox(str);
return FALSE;
}
}
BOOL CADORecordset::MovePrevious()
{
if(!IsOpen())
{
AfxMessageBox("应该先打开记录集!");
return FALSE;
}
try
{
m_set->MovePrevious();
return TRUE;
}
catch(_com_error &e)
{
CString str;
str=e.ErrorMessage();
str="17 "+str;
AfxMessageBox(str);
return FALSE;
}
}
BOOL CADORecordset::MoveNext()
{
if(!IsOpen())
{
AfxMessageBox("应该先打开记录集!");
return FALSE;
}
try
{
m_set->MoveNext();
return TRUE;
}
catch(_com_error &e)
{
CString str;
str=e.ErrorMessage();
str="18 "+str;
AfxMessageBox(str);
return FALSE;
}
}
BOOL CADORecordset::AddNew()
{
if(!IsOpen())
{
AfxMessageBox("应该先打开记录集!");
return FALSE;
}
try
{
m_set->AddNew();
return TRUE;
}
catch(_com_error &e)
{
CString str;
str=e.ErrorMessage();
str="19 "+str;
AfxMessageBox(str);
return FALSE;
}
}
BOOL CADORecordset::Delete()
{
if(!IsOpen())
{
AfxMessageBox("应该先打开记录集!");
return FALSE;
}
try
{
m_set->Delete(adAffectCurrent);
return TRUE;
}
catch(_com_error &e)
{
CString str;
str=e.ErrorMessage();
str="20 "+str;
AfxMessageBox(str);
return FALSE;
}
}
BOOL CADORecordset::DeleteField(long index)
{
if(!IsOpen())
{
AfxMessageBox("应该先打开记录集!");
return FALSE;
}
try
{
_variant_t ind;
ind.vt=VT_I4;
ind.lVal=index;
//删除指定的记录:
m_set->Fields->Delete(ind);
return TRUE;
}
catch(_com_error &e)
{
CString str;
str=e.ErrorMessage();
str="21 "+str;
AfxMessageBox(str);
return FALSE;
}
}
BOOL CADORecordset::Save(LPCTSTR fn)
{
if(!IsOpen())
{
AfxMessageBox("应该先打开记录集!");
return FALSE;
}
try
{
_variant_t vfn;
vfn=fn;
// m_set->Save(vfn,adPersistADTG);
return TRUE;
}
catch(_com_error &e)
{
CString str;
str=e.ErrorMessage();
str="22 "+str;
AfxMessageBox(str);
return FALSE;
}
}
long CADORecordset::GetMaxRecords()
{
if(!IsOpen())
{
AfxMessageBox("应该先打开记录集!");
return FALSE;
}
return m_set->MaxRecords;
}
void CADORecordset::SetPageSize(long psz)
{
if(!IsOpen())
{
AfxMessageBox("应该先打开记录集!");
return;
}
m_set->PageSize=psz;
}
long CADORecordset::GetPageSize()
{
if(!IsOpen())
{
AfxMessageBox("应该先打开记录集!");
return FALSE;
}
return m_set->PageSize;
}
long CADORecordset::GetPageCount()
{
if(!IsOpen())
{
AfxMessageBox("应该先打开记录集!");
return FALSE;
}
return m_set->PageCount;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -