#include "stdafx.h" #include <stdio.h> #include <windows.h> #include <objbase.h> #include <initguid.h> #include "adoid.h" #include "adoint.h" struct InitOle { InitOle(){::CoInitialize(NULL);} ~InitOle(){::CoUninitialize();} }_init_InitOle_; void main(int argc, char* argv[]) { //::CoInitialize(NULL); HRESULT hr = S_OK; ADORecordset *Rs1 = NULL; VARIANT Source; VARIANT Connect; VariantInit(&Source); VariantInit(&Connect); Source.vt = VT_BSTR; Source.bstrVal = ::SysAllocString(L"SELECT * FROM Employees"); Connect.vt = VT_BSTR; Connect.bstrVal = ::SysAllocString(L"Provider=SQLOLEDB;/ data source=127.0.0.1;/ initial catalog=Northwind;/ user id=sa; password=sa;"); hr = CoCreateInstance(CLSID_CADORecordset, NULL, CLSCTX_INPROC_SERVER, IID_IADORecordset, (LPVOID *)&Rs1 ); if ( SUCCEEDED(hr) ) ::MessageBox(NULL, "CoCreateInstance Success!", "", MB_OK); if(SUCCEEDED(hr)) { Rs1->put_CursorLocation(adUseClient); hr = Rs1->Open(Source, Connect, adOpenForwardOnly, adLockReadOnly, -1); if ( SUCCEEDED(hr) ) ::MessageBox(NULL, "Open Success!", "", MB_OK); long n = 0; Rs1->get_RecordCount(&n); Rs1->Close(); Rs1->Release(); Rs1 = NULL; printf("%d",n); } //::CoUninitialize(); }