| What You Need |
|---|
|
#include
namespace MSADOCE
{
#include
}
#include
The “namespace” argument will enable the Com Smart Pointer and define the “MSADOCE” namespace used later in the code.
First we have to do some COM Smart Pointer declarations:
MSADOCE::IADOCEConnection *iADOCEConn = NULL; //Connection Object MSADOCE::IADOCERecordset *iADOCERS = NULL; //Recordset Object MSADOCE::IADOCEFields *iADOCEFields = NULL; //Fields Object
The ADOCE.H does not define the two GUIDs (globally unique identifiers) correctly for the Recordset and the Connection interfaces. Therefore we declare both of them here:
IID IID__Recordset =
{0x113033F6,0xF682,0x11D2,{0xBB,0x62,0x00,0xC0,0x4F,0x68,0x0A,0xCC}};
IID IID__Connection =
{0x113033DE,0xF682,0x11D2,{0xBB,0x62,0x00,0xC0,0x4F,0x68,0x0A,0xCC}};
The following two lines declare the ADOCE connection and Recordset ProgID.
TCHAR tsADOCE30ConnProgID[]= TEXT("ADOCE.Connection.3.0");
TCHAR tsADOCE30RSProgID[]= TEXT("ADOCE.Recordset.3.0");
Now we can start filling the objects with life:
CLSID tClsid;
HRESULT hr;
VARIANT varConn1,varTSQL,varEmpty;
hr = CoInitializeEx(NULL,COINIT_MULTITHREADED);
if(!SUCCEEDED(hr)) return 0;
VariantInit(&varConn1);
VariantInit(&varTSQL);
VariantInit(&varEmpty);
hr = CLSIDFromProgID( tsADOCE30ConnProgID, &tClsid );
if (FAILED(hr)) return 0; // Error handling here: ADOCE3.0 not found
hr = CoCreateInstance (tClsid, NULL,
CLSCTX_INPROC_SERVER | CLSCTX_LOCAL_SERVER,
IID__Connection, (LPVOID *)&iADOCEConn);
if(iADOCEConn && !FAILED(hr))
{
The following lines will create an ADOCE connection object for the Microsoft Windows CE Object store. If you want to open a Pocket Access database (.CDB), you can enter the file name of the .CDB file as the first parameter of the “Open” method.
hr=iADOCEConn->put_Provider(TEXT("cedb"));
hr=iADOCEConn->Open(TEXT(""),TEXT(""),
TEXT(""),MSADOCE::adOpenUnspecified);
hr=CLSIDFromProgID( tsADOCE30RSProgID, &tClsid );
hr = CoCreateInstance (tClsid, NULL,
CLSCTX_INPROC_SERVER | CLSCTX_LOCAL_SERVER,
IID__Recordset,(LPVOID *)&iADOCERS);
if(iADOCEConn && !FAILED(hr))
{
The next three lines are very important. I get a lot of questions about how to attach the Connection object to the Recordset correctly. Here is how:
varConn1.pdispVal=iADOCEConn; varConn1.vt=VT_DISPATCH; hr=iADOCERS->put_ActiveConnection(varConn1);
We create a new table named “Book841” with integer, date, string, and long varbinary fields.
varTSQL.bstrVal=SysAllocString(
TEXT("CREATE TABLE Book841 (MyInt integer, MyDate,
datetime, MyString varchar(200), MyBlob long varbinary)"));
varTSQL.vt=VT_BSTR;
hr=iADOCERS->Open(varTSQL,varEmpty,
MSADOCE::adOpenForwardOnly,
MSADOCE:: adLockReadOnly,
MSADOCE::adCmdUnknown);
if (FAILED(hr)) return FALSE; // Error handling
SysFreeString(varTSQL.bstrVal);
And now if we want to add a row to the table, we have to open it with a dynamic, updateable cursor.
varTSQL.bstrVal=SysAllocString(
TEXT("select * from Book842"));
varTSQL.vt=VT_BSTR;
hr=iADOCERS->Open(varTSQL,varEmpty,
MSADOCE::adOpenDynamic ,
MSADOCE::adLockOptimistic ,
MSADOCE::adCmdUnknown);
if (FAILED(hr)) return FALSE; // Error handling
SysFreeString(varTSQL.bstrVal);
hr=iADOCERS->AddNew(varEmpty,varEmpty);
hr=iADOCERS->get_Fields(&iADOCEFields);
}
}
Before you can compile and link the sample, add the OLE32.LIB and OLEAUT32.LIB to the list of object/library modules in the project settings of your project.
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/10294527/viewspace-126774/ ,如需转载,请注明出处,否则将追究法律责任。