#import "C:\Program Files\Common Files\System\ADO\msado15.dll" \ no_namespace rename("EOF", "EndOfFile") |
CLSID clsid; HRESULT hr = ::CLSIDFromProgID(L"ADODB.Connection", &clsid); if(FAILED(hr)) {...} ::CoCreateInstance(clsid, NULL, CLSCTX_SERVER, IID_IDispatch, (void **) &pDispatch); if(FAILED(hr)) {...} |
#include <icrsint.h> //Include support for VC++ Extensions #import "C:\Program Files\Common Files\System\ADO\msado15.dll" \ no_namespace rename("EOF", "adoEOF") |
_COM_SMARTPTR_TYPEDEF(_Collection, __uuidof(_Collection)); |
_ConnectionPtr pMyConnect=NULL; HRESULT hr=pMyConnect.CreateInstance(__uuidof(Connection))); if(FAILED(hr))return; _bstr_t strConnect="Provider=SQLOLEDB; Server=server_name;" "Database=database_name; uid=user_name; pwd=password;"; //connecting to the database server now: try{pMyConnect->Open(strConnect,"","",NULL);} catch (_com_error &e) { ::MessageBox(NULL,e.Description(),"警告",MB_OK │ MB_ICONWARNING); } |
_bstr_t strConnect="DSN=datasource_name; Database=database_name; uid=user_name; pwd=password;"; |
_RecordsetPtr m_pRecordset; if(!FAILED(m_pRecordset.CreateInstance( __uuidof( Recordset ))) { m_pDoc->m_initialized=FALSE; return; } try{ m_pRecordset->Open(_variant_t("mytable"), _variant_t((IDispatch *)pMyConnect,true), adOpenKeyset, adLockOptimistic, adCmdTable); } catch (_com_error &e) { ::MessageBox(NULL,"无法打开mytable表。","提示", MB_OK │ MB_ICONWARNING); } |
try{ m_pRecordset->MoveFirst(); while(m_pRecordset->adoEOF==VARIANT_FALSE) { //Retrieve column's value: CString sName=(char*)(_bstr_t)(m_pRecordset->Fields->GetItem (_variant_t("name"))->Value); short cAge=(short)(m_pRecordset->Fields->GetItem (_variant_t("age"))->Value); //Do something what you want to do: ...... m_pRecordset->MoveNext(); } }//try catch (_com_error &e) { CString str=(char*)e.Description(); ::MessageBox(NULL,str+"\n又出毛病了。","提示", MB_OK │ MB_ICONWARNING); } |
CString sName=(char*)(_bstr_t)(m_pRecordset->Fields->GetItem (_variant_t("name"))->GetValue()); |
try{ m_pRecordset->MoveFirst(); while(m_pRecordset->adoEOF==VARIANT_FALSE) { m_pRecordset->Fields->GetItem (_variant_t("姓名"))->Value=_bstr_t("赵薇"); ...... m_pRecordset->Update(); m_pRecordset->MoveNext(); } }//try |
m_pRecordset->Fields->GetItem (_variant_t("姓名"))->PutValue(_bstr_t("赵薇")); |
// Add new record into this table: try{ if(!m_pRecordset->Supports(adAddNew)) return; m_pRecordset->AddNew(); m_pRecordset->Fields->GetItem (_variant_t("姓名"))->Value=_bstr_t("赵薇"); m_pRecordset->Fields->GetItem (_variant_t("性别"))->Value=_bstr_t("女"); m_pRecordset->Fields->GetItem (_variant_t("age"))->Value=_variant_t((short)20); m_pRecordset->Fields->GetItem (_variant_t("marry"))->Value=_bstr_t("未婚"); m_pRecordset->Update(); }//try catch (_com_error &e) { ::MessageBox(NULL, "又出毛病了。","提示",MB_OK │ MB_ICONWARNING); } |
_variant_t varName[4],narValue[4]; varName[0] = L"姓名"; varName[1] = L"性别"; varName[2] = L"age"; varName[3] = L"marry"; narValue[0]=_bstr_t("赵薇"); narValue[1]=_bstr_t("女"); narValue[2]=_variant_t((short)20); narValue[3]=_bstr_t("未婚"); const int nCrit = sizeof varName / sizeof varName[0]; // Create SafeArray Bounds and initialize the array SAFEARRAYBOUND rgsaName[1],rgsaValue[1]; rgsaName[0].lLbound = 0; rgsaName[0].cElements = nCrit; SAFEARRAY *psaName = SafeArrayCreate( VT_VARIANT, 1, rgsaName ); rgsaValue[0].lLbound = 0; rgsaValue[0].cElements = nCrit; SAFEARRAY *psaValue = SafeArrayCreate( VT_VARIANT, 1, rgsaValue ); // Set the values for each element of the array HRESULT hr1=S_OK.hr2=S_OK; for( long i = 0 ; i < nCrit && SUCCEEDED( hr1 ) && SUCCEEDED( hr2 );i++) { hr1=SafeArrayPutElement(psaName, &i,&varName[i]); hr2=SafeArrayPutElement(psaValue, &i,&narValue[i]); } // Initialize and fill the SafeArray VARIANT vsaName,vsaValue; vsaName.vt = VT_VARIANT │ VT_ARRAY; vsaValue.vt = VT_VARIANT │ VT_ARRAY; V_ARRAY(&vsaName) = psaName;//&vsaName->parray=psaName; //see definition in oleauto.h file. V_ARRAY(&vsaValue) = psaValue; // Add a new record: m_pRecordset->AddNew(vsaName,vsaValue); |
try{ m_pRecordset->MoveFirst(); while(m_pRecordset->adoEOF==VARIANT_FALSE) { CString sName=(char*)(_bstr_t)(m_pRecordset->Fields->GetItem (_variant_t("姓名"))->Value); if(::MessageBox(NULL,"姓名="+sName+"\n删除她吗?", "提示",MB_YESNO │ MB_ICONWARNING)==IDYES) { m_pRecordset->Delete(adAffectCurrent); m_pRecordset->Update(); } m_pRecordset->MoveNext(); } }//try catch (_com_error &e) { ::MessageBox(NULL,"又出毛病了。","提示",MB_OK │ MB_ICONWARNING); } |
_ConnectionPtr Conn1; _CommandPtr Cmd1; ParametersPtr *Params1 = NULL; // Not an instance of a smart pointer. _ParameterPtr Param1; _RecordsetPtr Rs1; try { // Create Connection Object (1.5 Version) Conn1.CreateInstance( __uuidof( Connection ) ); Conn1->ConnectionString = bstrConnect; Conn1->Open( bstrEmpty, bstrEmpty, bstrEmpty, -1 ); // Create Command Object Cmd1.CreateInstance( __uuidof( Command ) ); Cmd1->ActiveConnection = Conn1; Cmd1->CommandText = _bstr_t("SELECT * FROM mytable WHERE age< ?"); }//try |
Cmd1->ActiveConnection = Conn1; |
// Create Parameter Object Param1 = Cmd1->CreateParameter( _bstr_t(bstrEmpty), adInteger, adParamInput, -1, _variant_t( (long) 5) ); Param1->Value = _variant_t( (long) 5 ); Cmd1->Parameters->Append( Param1 ); |
// Open Recordset Object Rs1 = Cmd1->Execute( &vtEmpty, &vtEmpty2, adCmdText ); |
try{ m_pRecordset->Open((IDispatch *) Cmd1, vtMissing, adOpenStatic, adLockOptimistic, adCmdUnspecified); } catch (_com_error &e) { ::MessageBox(NULL,"mytable表不存在。","提示",MB_OK │ MB_ICONWARNING); } |
class CConnEvent : public ConnectionEventsVt { private: ULONG m_cRef; public: CConnEvent() { m_cRef = 0; }; ~CConnEvent() {}; STDMETHODIMP QueryInterface(REFIID riid, void ** ppv); STDMETHODIMP_(ULONG) AddRef(void); STDMETHODIMP_(ULONG) Release(void); STDMETHODIMP raw_InfoMessage( struct Error *pError, EventStatusEnum *adStatus, struct _Connection *pConnection); STDMETHODIMP raw_BeginTransComplete( LONG TransactionLevel, struct Error *pError, EventStatusEnum *adStatus, struct _Connection *pConnection); ...... }; |
STDMETHODIMP CConnEvent::raw_InfoMessage( struct Error *pError, EventStatusEnum *adStatus, struct _Connection *pConnection) { *adStatus = adStatusUnwantedEvent; return S_OK; }; |
STDMETHODIMP CConnEvent::QueryInterface(REFIID riid, void ** ppv) { *ppv = NULL; if (riid == __uuidof(IUnknown) ││ riid == __uuidof(ConnectionEventsVt)) *ppv = this; if (*ppv == NULL) return ResultFromScode(E_NOINTERFACE); AddRef(); return NOERROR; } STDMETHODIMP_(ULONG) CConnEvent::AddRef() { return ++m_cRef; }; STDMETHODIMP_(ULONG) CConnEvent::Release() { if (0 != --m_cRef) return m_cRef; delete this; return 0; } |
// Start using the Connection events IConnectionPointContainer *pCPC = NULL; IConnectionPoint *pCP = NULL; hr = pConn.CreateInstance(__uuidof(Connection)); if (FAILED(hr)) return; hr = pConn->QueryInterface(__uuidof(IConnectionPointContainer), (void **)&pCPC); if (FAILED(hr)) return; hr = pCPC->FindConnectionPoint(__uuidof(ConnectionEvents), &pCP); pCPC->Release(); if (FAILED(hr)) return; pConnEvent = new CConnEvent(); hr = pConnEvent->QueryInterface(__uuidof(IUnknown), (void **) &pUnk); if (FAILED(hr)) return rc; hr = pCP->Advise(pUnk, &dwConnEvt); pCP->Release(); if (FAILED(hr)) return; pConn->Open("dsn=Pubs;", "sa", "", adConnectUnspecified); |
pConn->Close(); // Stop using the Connection events hr = pConn->QueryInterface(__uuidof(IConnectionPointContainer), (void **) &pCPC); if (FAILED(hr)) return; hr = pCPC->FindConnectionPoint(__uuidof(ConnectionEvents), &pCP); pCPC->Release(); if (FAILED(hr)) return rc; hr = pCP->Unadvise( dwConnEvt ); pCP->Release(); if (FAILED(hr)) return; |