Ado.cpp文件
#include "stdafx.h"#include "Ado.h"
CAdo::CAdo(){ m_pConnect = NULL;}
CAdo::~CAdo(){}
bool CAdo::ADOConnect(CString strIP, CString strUserName, CString strPwd, CString strDBName, CString & strErr) //连接数据库,实例化智能指针,若有错误,则错误信息保存在strErr中{ try { HRESULT hr = m_pConnect.CreateInstance(__uuidof(Connection)); //实例化 if(FAILED(hr)) { strErr.Format((LPCTSTR)"Connect Create Instance Error."); return false; } } catch(_com_error * e) { strErr = e->ErrorMessage(); return false; } catch(...) { strErr = _T("Create connect instance unknown error"); return false; } CString strConn; strConn.Format(_T("Provider=SQLOLEDB; Server=%s; Database=%s; uid=%s; pwd=%s;"), strIP, strDBName, strUserName, strPwd); _bstr_t strConnect=(_bstr_t)strConn; //connecting to the database server now: try { if(FAILED(m_pConnect->Open(strConnect,_T(""),_T(""),NULL))) //连接数据库 { strErr.Format(_T("Connect DB Error.")); return false; } } catch (_com_error * e) { strErr = e->ErrorMessage(); return false; } catch(...) { strErr = _T("Connect DB unknown error"); return false; } return true;}
bool CAdo::ADODisconnect(CString & strErr) //关闭记录集,释放连接{ // TODO: Add your control notification handler code here try { if(m_pConnect != NULL && (m_pConnect->State == adStateOpen)) //关闭连接 { m_pConnect->Close(); m_pConnect = NULL; } } catch(_com_error* e) { strErr = e->ErrorMessage(); return false; } return true;}
CString CAdo::GetFilePath(int & iIndex, CString & strErr) //获取文件路径和该记录的ID,失败则返回空{ if(m_pConnect == NULL) // || m_pRecordset == NULL) { strErr = _T("查询时连接或记录集错误"); return _T(""); }
try { TCHAR szPath[256]; //存储文件路径的缓冲区 memset(szPath, 256, sizeof(TCHAR)); _CommandPtr m_pCommand; m_pCommand.CreateInstance(__uuidof(Command)); m_pCommand->ActiveConnection = m_pConnect; // 将库连接赋于它 m_pCommand->CommandText = _bstr_t("GetRecord"); m_pCommand->CommandType = adCmdStoredProc; _ParameterPtr param; //设置参数 param = m_pCommand->CreateParameter(_T("id"),adInteger, adParamOutput, sizeof(short), (short)iIndex);//返回参数,返回新建的网络的ID m_pCommand->Parameters->Append(param); param = m_pCommand->CreateParameter(_T("path"),adVarChar, adParamOutput, sizeof(szPath), _variant_t(szPath)); m_pCommand->Parameters->Append(param); m_pCommand->Execute(NULL, NULL,adCmdStoredProc); //执行存储过程,存储过程见另一篇文章
iIndex = (short)(m_pCommand->Parameters->GetItem(_T("id"))->GetValue()); //记录的ID
variant_t v; v = m_pCommand->Parameters->GetItem(_T("path"))->GetValue();//通过参数返回值 v.ChangeType(VT_BSTR,NULL); CString strPath = v.bstrVal; m_pCommand.Detach(); return strPath; } catch(_com_error * e) { strErr = e->ErrorMessage(); return _T(""); }}
bool CAdo::UpdateDB(int iIndex,CString strType, int iPageNum, CString & strErr) //完成后更新数据库,strType为状态位 if(iIndex < 1) { strErr = _T("记录的ID号小于1"); return false; } _CommandPtr m_pCommand; m_pCommand.CreateInstance(__uuidof(Command)); m_pCommand->ActiveConnection = m_pConnect; // 将库连接赋于它 CString strSQL; CString strTime = _T(""); CTime tm; tm=CTime::GetCurrentTime(); //获得系统当前时间作为完成时间 strTime.Format(_T("%d-%d-%d %d:%d:%d"), tm.GetYear(), tm.GetMonth(), tm.GetDay(), tm.GetHour(), tm.GetMinute(), tm.GetSecond()); strSQL.Format(_T("Update n_project set p_flag='%s',p_complete_date='%s',p_pages=%d, p_error='%s' where p_id = %d"), strType, strTime, iPageNum, strErr, iIndex); m_pCommand->CommandText = (_bstr_t)strSQL; // SQL语句 m_pCommand->Execute(NULL, NULL,adCmdText); // 执行更新语句 return true;}
Ado.h文件#import "C:/Program Files/Common Files/System/ADO/msado15.dll" /no_namespace rename("EOF", "EndOfFile")
class CAdo{public: CAdo(); ~CAdo(); bool ADOConnect(CString strIP, CString strUserName, CString strPwd, CString strDBName, CString & strErr); //连接数据库,实例化智能指针,若有错误,则错误信息保存在strErr中 bool ADODisconnect(CString & strErr); //关闭记录集,释放连接 CString GetFilePath(int & iIndex, CString & strErr); //获取文件路径和该记录的ID,失败则返回空 bool UpdateDB(int iIndex,CString strType, int iPageNum, CString & strErr);
private: _ConnectionPtr m_pConnect; //ADO连接 //_RecordsetPtr m_pRecordset; //ADO查询};