阅读:1399回复:0
C++ Example of an ADO Dataset#import "C:\program files\common files\system\ado\msado15.dll" \ rename ("EOF", "adoEOF") : : : /* * This must be here to ensure that COM is always initialized. * We could alternately put the CoInitialize and CoUninitialize * calls in InitInstance and ExitInstance of our application * class. */ static struct InitOle { InitOle() { ::CoInitialize(NULL); } ~InitOle() { ::CoUninitialize(); } } _init_InitOle_; inline void TESTHR(HRESULT x) {if FAILED(x) _com_issue_error(x);}; : : : void CMapXView::OnADODSet() { // Note: instead of having all these ADODB:: qualifiers, // we could have just put "using namespace ADODB;" at the top // of this file. ADODB::_RecordsetPtr rsPtr; HRESULT hr; COptionalVariant optVt; CMapXDataset ds; COleVariant rsVt; // Create a new Recordset object TESTHR(hr = rsPtr.CreateInstance(__uuidof(ADODB::Recordset))); // Open the ADO recordset to import the USA data. // Change the "Data Source=" line if MapX was installed in a // different location. hr = rsPtr->Open("Select * from USA", "Provider=Microsoft.Jet.OLEDB.3.51;" "Data Source=C:\\Program Files\\MapInfo\MapX 5.0\\Data\\MapStats.mdb", ADODB::adOpenStatic, ADODB::adLockReadOnly, ADODB::adCmdText); // Make sure the Open() succeeded. TESTHR(hr); // Make the rsVt variant point to our recordset object. rsVt.vt = VT_DISPATCH; rsVt.pdispVal = rsPtr; rsVt.pdispVal->AddRef(); try { // Add a dataset to the map and create a simple theme //from it. ds = m_ctrlMapX.GetDatasets().Add(miDataSetADO, rsVt, COleVariant("ADOSet"), optVt, optVt, COleVariant("USA"), optVt, optVt); ds.GetThemes().Add(miThemeRanged, "TOTPOP"); } catch(COleDispatchException* e) { e->ReportError(); e->Delete(); } catch(COleException* e) { e->ReportError(); e->Delete(); } } |
|
|