在正式進入本主題前,讓我們先來看一下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);
}
}
本例完成的功能就是取得數據庫中某表的記錄數。