Basics of using ADOCE on Windows CE Devices

For the sake of argument, it should be noted that I am not a Database expert.
I have experience using ADOCE because of a project I was working on in the recent
past. It was semi-difficult to use at first, but once I got the hang of it, it
was actually very simple. This article is just a place to start – and will hopefully
help you in your own applications.

Screen shot

Environment: VC6 NT4 SP4, CE 2.11

Tested on: HPC PRO Device running WinCE v2.11 only

This code will be useful to you if you have tried using ADOCE, but never got anything
to work. It is not meant to be integrated into any application, but rather it should be
used for demonstrative purposes. It clearly shows the basics of how to use the technology.

Setting up an ADOCE project using Visual C++ 6.0 is rather simple.
Assuming that you have downloaded and installed the ADOCE SDK from Microsoft,
you are ready to use it in your Windows CE Database applications. The sample
that I have provided is a *very* simple one illustrating how to instantiate the
proper COM objects, and the basics of how to interface with them (in a very simple
example).

NOTE: If you do not have the ADOCE SDK, the sample code that can be downloaded below
contains the 2 important files that you need (ADOCE_I.C and ADOCE.H).

As you can see in the code below labelled Part 1, the first step is to initialize
COM with our application. This is standard for using COM in any application, and
does not apply only to ADOCE applications.

Part 2 below shows us how we can instantiate a RecordSet object. RecordSets are
used in ADOCE to open database sources, execute SQL commands and queries, and
for accessing the fields within tables.

The code in Part 3 is explained below it.



//Part 1:
//-------

if (FAILED(CoInitializeEx(NULL, COINIT_MULTITHREADED))) {
	AfxMessageBox(_T("Error initializing COM"));
	return E_FAIL;
}

//Part 2:
//-------

if (FAILED(CoCreateInstance(CLSID_ADOCERecordset, NULL,
                            CLSCTX_INPROC_SERVER, IID_IADOCERecordset,
			    (void**)&m_pIRecordSet))) {
	AfxMessageBox(_T("Could not load ADOCE"));
	return E_FAIL;
}

//Part 3:
//-------

//Build up the SQL Query
CString strQuery =
	_T("SELECT ToyTable.name FROM ToyTable");

//Build up the Variant data types
query.SetString((LPCTSTR)strQuery,VT_BSTR);
connStr.SetString((LPCTSTR)m_strFilePath,VT_BSTR);

//Execute the SQL query
hr = getRecordSet()->Open(query,connStr,cte,lte,adCmdText);
if (FAILED(hr)) {
	return FALSE;
}

Let’s take a closer look at Part 3 (as shown above). The first part of executing an SQL
query is to form the query. We do this using the simple query “SELECT ToyTable.name FROM ToyTable”.
Since ADOCE is compatible with other languages such as Visual Basic, the data types
used most commonly are data types that can be shared across languages (namely, Variants).
MFC provides a wrapper for the Variant type called COleVariant. After constructing the
query in a normal (MFC in this case) string, we set the value of the COleVariant to contain
the query. The connStr variable may look a little funny up there, but what it’s used for
is telling the system that we want to use the database located inside of a file. As you
can see, we’re setting the value of it to be m_strFilePath, which is a path pointing to
an MS-Access database file on the device. The next logical step is to execute the query,
and so we do. Note the last parameter of the Open(…) call. To execute and SQL query, it
needs to be set to “adCmdText”. For just opening tables, use “adCmdTable” – but all of that
is in the VB documentation (look for RecordSet.Open).

Gathering information from the RecordSet object is another story all together. I recommend two
things in your ventures with ADOCE. 1.) Read and follow the Visual Basic documentation,
because although it’s Visual Basic, the ADOCE model works very similarly. 2.) When in
doubt, dig through the SDK source files (namely ADOCE_I.C and ADOCE.H). That way, you can see
for yourself what the difference between the ADOCE API and the VB ADO API is.

Of course, I also recommend carefully stepping through the sample code included with this
document because it shows a simple case as well.

All of the code samples shown above are included in the sample code available for
download below. Also included below is the MS-Access file containing the ToyTable used
with this example (db1.mdb).

NoteTo use this file on the device, copy it to the HPC/PRO device
and allow CE services to convert it from desktop to Pocket Access format.

Downloads

Download demo project: exadoce_demo.zip – 21 Kb

Download source: exadoce_src.zip – 15 Kb

MS-Access file: exadoce_db.zip – 5 KB

More by Author

Must Read