// StdAfx.h
#import "c:\program files\common files\system\ado\msado15.dll" \
no_namespace \
rename ("EOF", "adoEOF")
BOOL CTry01App::InitInstance()
{
// {
if(!AfxOleInit()) {
AfxMessageBox("Could not initialize COM dll");
return FALSE;
}
// }
AfxEnableControlContainer();
...
}
class CTry01Dlg : public CDialog
{
// {
_ConnectionPtr m_pConnection;
_RecordsetPtr m_MySet;
// }
// Construction
public:
CTry01Dlg(CWnd* pParent = NULL); // standard constructor
...
};
BOOL CTry01Dlg::OnInitDialog()
{
...
SetIcon(m_hIcon, FALSE); // Set small icon
// TODO: Add extra initialization here
// {
HRESULT hr;
hr = m_pConnection.CreateInstance(__uuidof(Connection));
if (FAILED (hr)) {
AfxMessageBox ("Can't create an instance of Connection");
return TRUE;
}
hr = m_MySet.CreateInstance(__uuidof(Recordset));
if (FAILED (hr)) {
AfxMessageBox ("Can't create an instance of Recordset");
return TRUE;
}
hr = m_pCommand.CreateInstance (__uuidof(Command));
if (FAILED (hr)) {
AfxMessageBox ("Can't create an instance of Command");
return TRUE;
}
try
{
CString sConnect;
sConnect = "DRIVER={MySQL ODBC 5.1 Driver};SERVER=localhost;DB=my_db;User=userid;Password=userpass;Port=3306;";
// sConnect = "DRIVER={MySQL ODBC 3.51 Driver};SERVER=localhost;DB=my_db;User=userid;Password=userpass;Port=3306;";
_bstr_t strConnection;
strConnection = sConnect;
m_pConnection->CommandTimeout = 1;
m_pConnection->Open(strConnection, _bstr_t(L""), _bstr_t(L""), adConnectUnspecified);
} catch (_com_error *e) {
CString Error = e->ErrorMessage();
...
return TRUE;
} catch(...) {
...
return TRUE;
}
try
{
CString sQry;
sQry.Format("select field1, field2 from table1 ");
m_MySet->Open(LPCTSTR(sQry),
m_pConnection.GetInterfacePtr(), adOpenDynamic, adLockOptimistic, adCmdText);
CString field1, field2;
_variant_t holder;
while (!m_MySet->adoEOF) {
holder = m_MySet->GetCollect("field1");
if (holder.vt != VT_NULL)
field1 = (char*)_bstr_t(holder);
else
field1 = "NULL";
holder = m_MySet->GetCollect("field2");
if(holder.vt != VT_NULL)
field2 = (char*)_bstr_t(holder);
else
field2 = "NULL";
...
m_MySet->MoveNext();
}
m_MySet->Close();
} catch (_com_error *e) {
m_MySet->Close();
CString Error = e->ErrorMessage();
...
return TRUE;
} catch(...) {
m_MySet->Close();
...
return TRUE;
}
// }
return TRUE; // return TRUE unless you set the focus to a control
}
...
void CTry01Dlg::OnDestroy()
{
// {
m_pConnection->Close();
// }
CDialog::OnDestroy();
// TODO: Add your message handler code here
}