1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889 |
- #include "stdafx.h"
- #include "AdoInterface.h"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #endif
- CAdoInterface::CAdoInterface()
- {
- }
- CAdoInterface::~CAdoInterface()
- {
- CloseDataBase();
- }
- //************************************//
- // [函数]:CreateInstance
- // [描述]:初始化ado
- // [参数]:
- // [返回]:S_OK成功,S_FALSE失败
- //************************************//
- HRESULT CAdoInterface::CreateInstance()
- {
- HRESULT hr = CoInitialize(NULL);
- if(!SUCCEEDED(hr))
- return S_FALSE;
- hr = m_pContPtr.CreateInstance(__uuidof(Connection));
- if(!SUCCEEDED(hr))
- return S_FALSE;
- return S_OK;
- }
- //************************************//
- // [函数]:OpenDataBase
- // [描述]:打开数据库(如:ACCSESS)
- // [参数]:
- // pConnectStr: 连接数库的信息;
- // pPWD: 数据库密码s
- // ResultMsg 返回错误信息
- // [返回]:S_OK成功,S_FALSE失败
- //************************************//
- HRESULT CAdoInterface::OpenDataBase(const TCHAR* pConnectStr, const TCHAR* pUserID, const TCHAR* pPWD, CString& errMsg)
- {
- if(pConnectStr == NULL || pPWD == NULL)
- return S_FALSE;
- _variant_t returnValue;
- char ErrMessage[512] = {0};
- try
- {
- /*
- ACCESS97 Provider=Microsoft.Jet.OLEDB.3.51
- ACCESS2000 Provider=Microsoft.Jet.OLEDB.4.0
- */
- //m_pContPtr->Open(_T("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=PhoneBook.mdb"), _T(""), _T(""), adModeUnknown);
- m_pContPtr->Open(pConnectStr, pUserID, pPWD, adModeUnknown);
- }
- catch(_com_error e)
- {
- /*CString strErr = _T("打开数据库失败:");
- strErr += e.ErrorMessage();
- strErr += _T(",");
- strErr += e.Description().GetBSTR();
- WChar2Char(ErrMessage, strErr.GetBuffer());
- errMsg = _com_util::ConvertStringToBSTR(ErrMessage);*/
- return S_FALSE;
- }
- return S_OK;
- }
- //************************************//
- // [函数]:CloseDataBase
- // [描述]:关闭数据库
- // [参数]:
- // [返回]:
- //************************************//
- void CAdoInterface::CloseDataBase()
- {
- if(m_pContPtr)
- {
- if(m_pContPtr->State)
- m_pContPtr->Close();
- m_pContPtr = NULL;
- }
- }
|