⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 adomodule.cpp

📁 wince 下面有关ADOCE方面的库操作code!
💻 CPP
字号:
/***********************************************************
 *
 * Written by Neill Miller of Northwestern University
 *
 * Contact author at neillm@cs.nwu.edu
 *
 * ADOCE Example using the MFC.  Targeted for HPC/PRO devices
 * running Windows CE 2.11+.
 *
 * This example is not an all purpose Database wrapper.  It
 * illustrates how to use ADOCE for database access, and assumes
 * to be working with a database that contains one table:
 *
 * The Relational schema for the table is:
 *
 * ToyTable(uid int, name text)
 *
 * This example shows how to access these tables within an
 * MS-Access database file (.cdb) on an HPC/PRO device.
 *
 * NOTE: ADOCE_I.C and ADOCE.H were included with the ADOCE SDK
 ***********************************************************/

#include "stdafx.h"

#include "ADOModule.h"

//Constructor - must call Initialize before using this class
ADOModule::ADOModule()
{
	m_bInit			= FALSE;
	m_pIFields		= NULL;
	m_pIRecordSet	= NULL;
}

//Must be called before using this class.
//The COM components are initialized here.
HRESULT ADOModule::Initialize()
{
	TCHAR		szFilter[] = _T("MS-Access Files (*.cdb)|*.cdb|");

	//You are required to pick the database file to be working with
	CFileDialog	cfd(TRUE,_T("cdb"),NULL,OFN_FILEMUSTEXIST,
				(LPCTSTR)szFilter,NULL);

	if (cfd.DoModal() == IDOK) {

		//Store the path of the database file for later use
		m_strFilePath	= cfd.GetPathName();

		//Initialize the COM system:
		//Note that the Multithreaded flag is the only 
		//one supported in Windows CE at this time.
		if (FAILED(CoInitializeEx(NULL, COINIT_MULTITHREADED))) {
			AfxMessageBox(_T("Error initializing COM"));
			return E_FAIL;
		}

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

		//Change class state to initialized state
		m_bInit = TRUE;
		
		//Make sure that we're dealing with a database that this
		//sample application can work with.
		if (!VerifyCompatible(TRUE)) {
			return E_FAIL;
		}

		return S_OK;
	}
	return E_FAIL;
}

ADOModule::~ADOModule()
{
	//Release the Components used
    if (m_pIFields)
        m_pIFields->Release();

    if (m_pIRecordSet)
        m_pIRecordSet->Release();

	//Shut down COM
	CoUninitialize();
}

BOOL ADOModule::UpdateFields()
{
	BOOL bRetVal = FALSE;

	if (isInitialized()) {
		if (FAILED(m_pIRecordSet->get_Fields(&m_pIFields))) {
			bRetVal = FALSE;
		} else {
			bRetVal = TRUE;
		}
	}
	return bRetVal;
}

//Tells if the selected file is Compatible with this sample
//application (i.e. has the proper tables within the database).
//If incoming parameter is TRUE, it will show a dialog box
//with the results - and a record count of the tables.
BOOL ADOModule::VerifyCompatible(BOOL bShow)
{
	long			size = 0;
	HRESULT			hr;
	COleVariant		source;
	COleVariant		connStr;
	CursorTypeEnum	cte	= adOpenKeyset;
	LockTypeEnum	lte	= adLockReadOnly;

	//Check to make sure that the 'ToyTable' table exists
	source.SetString(_T("ToyTable"),VT_BSTR);
	connStr.SetString((LPCTSTR)m_strFilePath,VT_BSTR);

	//Open the table
	hr = getRecordSet()->Open(source,connStr,cte,lte,adCmdTable);
	if (FAILED(hr)) {
		return FALSE;
	}

	//And get the record Count
	hr = getRecordSet()->get_RecordCount(&size);
	if (FAILED(hr)) {
		return FALSE;
	}

	CString strMsg;
	strMsg.Format(_T("'ToyTable' table opened properly with %ld records.\r\n"),size);

	//Close the 'ToyTable' recordSet
	hr = getRecordSet()->Close();
	if (FAILED(hr)) {
		return FALSE;
	}

	if (bShow) {
		AfxMessageBox(strMsg);
	}
	return TRUE;
}

//This is where the query takes place.  For the parameters
//to the Open call, be sure to look up RecordSet.Open in the
//Visual Basic documentation.
BOOL ADOModule::DoQuery(CString& strResult)
{
	HRESULT			hr;
	COleVariant		source;
	COleVariant		connStr;
	COleVariant		query;
	COleVariant		str;
	COleVariant		value;
	VARIANT_BOOL	eof		= FALSE;
	VARIANT_BOOL	vFalse	= FALSE;
	CursorTypeEnum	cte		= adOpenForwardOnly;
	LockTypeEnum	lte		= adLockReadOnly;
	IADOCEField		*field	= NULL;
	IADOCEFields	*fields	= NULL;

	//reset the incoming parameter
	strResult = _T("");

	//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;
	}
	
	//Format results of the query
	//***************************

	//First, let's grab the names of the people
	str.SetString(_T("name"),VT_BSTR);

	do {
		//Retrieve the name field from the recordSet
		fields = getFields();
		if (fields == NULL) {
			AfxMessageBox(_T("Internal error"));
			return FALSE;
		}
		hr = fields->get_Item(str,&field);
		if (FAILED(hr)) {
			return FALSE;
		}

		//Get the string name of the "name"
		hr = field->get_Value(&value);
		if (FAILED(hr)) {
			return FALSE;
		}

		//Grab the BSTR and convert it to a CString
		strResult += _T("ToyTable.Name = ");
		strResult += CString(value.bstrVal);
		strResult += _T("\r\n");

		//Move to the next record
		getRecordSet()->MoveNext();

		//Check to see if we're at the end yet
		getRecordSet()->get_EOF(&eof);
	} while( eof == vFalse );


	//*************************************
	//And then close the recordSet
	hr = getRecordSet()->Close();
	if (FAILED(hr)) {
		return FALSE;
	}

	return TRUE;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -