WAP之家:为您提供最全最新的WAP技术,CP.SP.3G等行业资讯。 WAP之家交流论坛全新开放 点击进入>>
WAP资讯 | 3G动态 | SP动态 | 运营商动态 | 内容商动态 | 制造商动态 | 论坛讨论>> 每次自动访问
WAP技术 | WAP源码 | 手机编程 | 手机源码 | 无线技术 | J2ME技术 | 手机软件 添加到收藏夹
IVR技术 | SP资料 | SMS MMS技术 | 商业方案 | IVR下载 | 书籍教程 | 工具软件 语言:繁體中文

WAP之家技术文章手机编程Win Mobile程序开发ADOCE 的 Pure C++ 用法(英)

ADOCE 的 Pure C++ 用法(英)
作者:Microsoft  来源:Microsoft  发布时间:2005-12-21 16:50:13

Using ADOCE with Pure C++…

Most applications these days require some form of data storage, and Microsoft recommends using Microsoft® ActiveX® Data Objects for the Windows® CE operating system (ADOCE). Most samples on ADOCE are written for Microsoft Visual Basic® developers, and there are not many samples out there showing how to use ADOCE with C++. This article takes you through the minimal steps necessary to access ADOCE from Microsoft Visual C++®.

What You Need
  • Microsoft eMbedded Visual Tools.
  • Microsoft eMbedded Visual C++.

Gotchas

ADOCE 2.0, ADOCE 3.0, and ADOCE 3.1 have different ProgIDs you have to watch for when you use the sample below. If you are not sure what ADOCE version is running on your device, you can try to create an ADOCE 3.1 connection object first. If that fails, try to create an ADOCE 3.0 connection object. If your device does not support 3.0 or 3.1, then you should install the latest version for the platform rather then use ADOCE 2.0. ADOCE 2.0 is not fully compatible to ADOCE 3.0 and your code might not work correctly. You can download the latest version of ADOCE 3.1.

Languages Supported

All languages

The Includes…

To include the ADOCE smart pointers, add the following code to the top of your source file:
#include 
namespace MSADOCE
{
#include 
}
#include 

The “namespace” argument will enable the Com Smart Pointer and define the “MSADOCE” namespace used later in the code.

The Code…

The following code snipplet will create an ADOCE connection and an ADOCE recordset, set the connection to the recordset, create a new table, insert a record, and read that record.

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.

What Storage Engine Should I Use?

ADOCE lets you access the Object Store, Microsoft Pocket Access files, and any other storage engine that supports the OLEDB layer for Windows CE. Microsoft recently released Microsoft SQL Server 2000 Windows CE Edition (SQL Server CE). It ships with the developer"s edition of SQL Server 2000 and is a great storage engine for all kind of applications. Since it is about 10 times faster then Pocket Access, this is the storage engine I recommend for any serious developer. And best of all—the runtime of the SQL Server CE is free.
[] [返回上一页] [打 印]
文章评论

用户名: 查看更多评论

分 值:100分 85分 70分 55分 40分 25分 10分 0分

内 容:

         (注“”为必填内容。) 验证码: 验证码,看不清楚?请点击刷新验证码