在正式进入本主题前,让我们先来看一下COM API编程
#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[])
{
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 Customers");
Connect.vt = VT_BSTR;
Connect.bstrVal = ::SysAllocString(L"Provider=SQLOLEDB; data source=127.0.0.1;initial catalog=Northwind; user id=sa; password=;");
hr = CoCreateInstance(CLSID_CADORecordset, NULL, CLSCTX_INPROC_SERVER, IID_IADORecordset, (LPVOID *)&Rs1 );
if(SUCCEEDED(hr))
{
Rs1->put_CursorLocation(adUseClient);
hr = Rs1->Open(Source, Connect, adOpenForwardOnly, adLockReadOnly,-1);
long n;
Rs1->get_RecordCount(&n);
Rs1->Close();
Rs1->Release(); Rs1 = NULL;
::MessageBox(NULL, "Success!", "", MB_OK);
printf("%d",n);
}
}
本例完成的功能就是取得数据库中某表的记录数。