1. StdAfx.h에 추가
#import "C:\Program Files\Common Files\System\ado\msado15.dll" rename("EOF", "EndOfFile") no_namespace
2. 생성자에 CoInitialize(0); 추가
이와 반대로 소멸자엔 CoUninitialize();를 추가
3. AdoObject.h에 추가
_ConnectionPtr m_pConnection;
_CommandPtr m_pCommand;
_RecordsetPtr m_pRecordset;
4. AdoObject.cpp에 추가
// ADO 접속
bool CAdoObject::ADOConnect(CString ip, CString dbname, CString userid, CString password)
{
try
{
char cString[200] = "";
sprintf(cString, "Provider=SQLOLEDB;Password=%s;Persist Security Info=false;User ID=%s;Initial Catalog=%s;Data Source=%s",
password, userid, dbname, ip);
HRESULT hr = m_pConnection.CreateInstance(__uuidof(Connection));
if( SUCCEEDED(hr) )
{
m_pConnection->ConnectionString = _bstr_t(cString);
m_pConnection->ConnectionTimeout = 10;
_bstr_t btEmpty=static_cast<_bstr_t>("");
m_pConnection->Open(m_pConnection->ConnectionString, btEmpty, btEmpty, adModeUnknown);
}
else
{
// -2147221008 CoInitialize(NULL); 를 추가 해야 됨.
// -2147467262 해당 인터페이스를 지원하지 않습니다 msado15.dll버전문제임. win2003서버에서 복사해와서 빌드하면 됨.
TRACE("Error! m_pConnection.CreateInstance hr[%d]", hr);
return false;
}
hr = m_pRecordset.CreateInstance(__uuidof(Recordset));
if( FAILED(hr) )
{
TRACE("Error! m_pRecordset.CreateInstance hr[%d]", hr);
return false;
}
m_pRecordset->CursorType = adOpenDynamic;
hr = m_pCommand.CreateInstance(__uuidof(Command));
if( FAILED(hr) )
{
TRACE("Error! m_pCommand.CreateInstance hr[%d]", hr);
return false;
}
m_pCommand->ActiveConnection = m_pConnection;
}
catch(_com_error &e)
{
DumpError(e);
return false;
}
return true;
}
// Ado 접속해제
bool CAdoObject::ADODisconnectEx()
{
try
{
//if( m_pRecordset )
//{
// m_pRecordset->Close();
// m_pRecordset = NULL;
//}
if( m_pCommand != NULL )
{
m_pCommand.Release();
m_pCommand = NULL;
}
if( m_pConnection )
{
m_pConnection->Close();
m_pConnection = NULL;
}
}
catch(_com_error &e)
{
DumpError(e);
return false;
}
return true;
}
void CAdoObject::DumpError(const _com_error &e)
{
_bstr_t btSource(e.Source());
_bstr_t btDescription(e.Description());
m_strLastError = "";
m_strLastError.Format("DumpError [%s], [%s]", (LPCTSTR)btSource, (LPCTSTR)btDescription);
TRACE((LPTSTR)(LPCTSTR)m_strLastError);
//AfxMessageBox(m_strLastError);
}
'MFC' 카테고리의 다른 글
[MFC] CreateSoundBuffer Error -2147024809 (0) | 2019.07.11 |
---|---|
[MFC] 메모리가 부족합니다 (0) | 2019.07.10 |
[MFC] 커맨드 인자(cmd argument) 사용하기 (0) | 2016.11.24 |