📄 dbaccess.cpp
字号:
// DBAccess.cpp: implementation of the DBAccess class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "DBAccess.h"
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
DBAccess::DBAccess()
{
m_fConnected=false;
//m_strSource="Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=sqpb;Data Source=laptop\\sqpb";
//m_strUser="";
//m_strPwd="";
m_pCmd.CreateInstance(__uuidof(Command));
m_pRst.CreateInstance(__uuidof(Recordset));
m_strStepName="Construction";
m_strObjName="";
}
DBAccess::~DBAccess()
{
}
int DBAccess::execute()
{
int intReturnCode=0;
if(!m_fConnected){
m_strErrMsg="***错误:数据库尚未连接";
return -1;
}
try{
m_strStepName="startbr";
m_pCmd->ActiveConnection=m_pCon;
m_pCmd->CommandText=(_bstr_t)m_strSQL;
m_pRst=m_pCmd->Execute(NULL,NULL,adCmdText);
m_fConnected=true;
}
catch(_com_error e){
m_strErrMsg.Format("***错误:执行命令出错(%s):%s",m_strStepName,PrintError());
return -1;
}
return 0;
}
int DBAccess::readnext()
{
int intReturnCode=0;
if(!m_fConnected){
m_strErrMsg="***错误:数据库尚未连接";
return -1;
}
try{
m_pRst->MoveNext();
}
catch(_com_error e){
m_strErrMsg.Format("***错误:记录集读取次笔出错:%s",PrintError());
return -1;
}
intReturnCode=bindvalue();
if(intReturnCode<0)m_strErrMsg.Format("***错误:获取表栏位值出错(%s)",m_strStepName);
return intReturnCode;
}
int DBAccess::endbr()
{
if(!m_fConnected)return -1;
m_pRst->Close();
return 0;
}
int DBAccess::insertrec()
{
int intReturnCode=0;
if(!m_fConnected){
m_strErrMsg="***错误:数据库尚未连接";
return -1;
}
HRESULT hr;
try{
m_strStepName="AddNew";
hr=m_pRst->AddNew();
m_strStepName="SetValue";
intReturnCode=setvalue();
if(intReturnCode<0){
m_strErrMsg.Format("***错误:设置表栏位值出错(%s)",m_strStepName);
return intReturnCode;
}
m_strStepName="Update";
hr=m_pRst->Update();
}
catch(_com_error e){
m_strErrMsg.Format("***错误:新增记录出错(%s):%s",m_strStepName,PrintError());
return -1;
}
return 0;
}
int DBAccess::updaterec()
{
int intReturnCode=0;
HRESULT hr;
if(!m_fConnected){
m_strErrMsg="***错误:数据库尚未连接";
return -1;
}
if(m_pRst->EndOfFile){
m_strErrMsg=" 警告:记录集到尾";
return 1;
}
try{
m_strStepName="SetValue";
intReturnCode=setvalue();
if(intReturnCode<0){
m_strErrMsg.Format("***错误:设置表栏位值出错(%s)",m_strStepName);
return intReturnCode;
}
m_strStepName="Update";
hr=m_pRst->Update();
}
catch(_com_error e){
m_strErrMsg.Format("***错误:更新数据出错(%s):%s",m_strStepName,PrintError());
return -1;
}
return 0;
}
int DBAccess::deleterec()
{
int intReturnCode=0;
HRESULT hr;
if(!m_fConnected){
m_strErrMsg="***错误:数据库尚未连接";
return -1;
}
if(m_pRst->EndOfFile){
m_strErrMsg=" 警告:记录集到尾";
return 1;
}
try{
m_strStepName="Delete";
hr=m_pRst->Delete(adAffectCurrent);
}
catch(_com_error e){
m_strErrMsg.Format("***错误:删除数据出错(%s):%s",m_strStepName,PrintError());
return -1;
}
return 0;
}
int DBAccess::startbr()
{
int intReturnCode=0;
HRESULT hr;
_bstr_t query=m_strSQL;
try{
m_strStepName="startbrforupdate";
//hr=m_pRst->Open((_bstr_t)m_strSQL,m_strSource,adOpenDynamic,adLockOptimistic,adCmdText);
hr=m_pRst->Open((_bstr_t)m_strSQL,m_pCon.GetInterfacePtr(),adOpenDynamic,adLockOptimistic,adCmdText);
//hr=m_pRst->Open((_bstr_t)m_strSQL,_variant_t((IDispatch *)m_pCon,true),adOpenDynamic,adLockOptimistic,adCmdText);
m_fConnected=true;
}
catch(_com_error e){
m_strErrMsg.Format("***错误:打开记录集出错(%s):%s",m_strStepName,PrintError());
return -1;
}
intReturnCode=bindvalue();
if(intReturnCode<0)m_strErrMsg="***错误:获取表栏位值出错";
return intReturnCode;
}
/*
//////////////////////////////////////////////////////////////////////
// 日期处理
//////////////////////////////////////////////////////////////////////
CString DBAccess::datestrproc(CString strInputTime)
{
struct tm tmTime;
CString strDate,strTime,strBuffer;
CString strYear,strMonth,strDay,strHour,strMinute,strSecond;
int intStart,intLength;
intLength=strInputTime.GetLength();
intStart=strInputTime.FindOneOf(" ");
if(intStart==-1){
strDate=strInputTime;
strTime="00:00:00";
}
else{
strDate=strInputTime.Left(intStart);
strTime=strInputTime.Right(intLength-intStart-1);
}
strYear=strDate.Left(4);
intLength=strDate.GetLength();
strBuffer=strDate.Right(intLength-5);
intStart=strBuffer.FindOneOf("-");
strMonth=strBuffer.Left(intStart);
intLength=strBuffer.GetLength();
strDay=strBuffer.Right(intLength-intStart-1);
intStart=strTime.FindOneOf(":");
strHour=strTime.Left(intStart);
strMinute=strTime.Mid(intStart+1,2);
strSecond=strTime.Mid(intStart+4,2);
tmTime.tm_year=atoi((LPCTSTR)strYear);
tmTime.tm_mon=atoi((LPCTSTR)strMonth);
tmTime.tm_mday=atoi((LPCTSTR)strDay);
tmTime.tm_hour=atoi((LPCTSTR)strHour);
tmTime.tm_min=atoi((LPCTSTR)strMinute);
tmTime.tm_sec=atoi((LPCTSTR)strSecond);
strBuffer.Format("%04d-%02d-%02d %02d:%02d:%02d",tmTime.tm_year,tmTime.tm_mon,tmTime.tm_mday,tmTime.tm_hour,tmTime.tm_min,tmTime.tm_sec);
return strBuffer;
}
*/
///////////////////////////////////////////////////////////
// //
// PrintError Function //
// //
///////////////////////////////////////////////////////////
CString DBAccess::PrintError()
{
// Print Provider Errors from Connection object.
// pErr is a record object in the Connection's Error collection.
ErrorPtr pErr = NULL;
long nCount = 0;
long i = 0;
CString strErrMsg;
if( (m_pCon->Errors->Count) > 0)
{
nCount = m_pCon->Errors->Count;
// Collection ranges from 0 to nCount -1.
for(i = 0; i < nCount; i++)
{
pErr = m_pCon->Errors->GetItem(i);
strErrMsg.Format("%x--%s\n", pErr->Number,(LPCSTR) pErr->Description);
}
}
return strErrMsg;
}
int DBAccess::setvalue()
{
return 0;
}
int DBAccess::bindvalue()
{
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -