ClearBox Server™ v1.2 Developer's Guide

Step 3. Creating CDatabase Class

In this step you will create class managing connections to MS Access database.

1. Right-click TestExtn classes in ClassView and select New Class... from the context menu. Select Generic Class class type and set CDatabase name for the class. Click OK.

2. Create CDatabase private member m_Session with CSession type. This variable will store an object representing a session established with the data source.

3. Add support of ATL OLE DB templates to the project. Open stdafx.h file (use FileView) and add line

 #include <atldbcli.h> 

after the line

 #include <atlbase.h> 

4. Create public CDatabase class method called Session which will provide access to the encapsulated session object:

 CSession& Session() 
{ 
	return m_Session;
}

5. Create public CDatabase class method called Open which will open session with database specified as method argument:

 HRESULT CDatabase::Open(LPCTSTR dataFile) 
{
	CDataSource db; 
	CDBPropSet dbinit(DBPROPSET_DBINIT); 
	dbinit.AddProperty(DBPROP_INIT_DATASOURCE, dataFile);
	HRESULT hr=db.Open(_T("Microsoft.Jet.OLEDB.4.0"), &dbinit); 
	if (FAILED(hr)) 
		return hr; 
	return m_Session.Open(db); 
}

6. Create public CDatabase class method called Close which will close session established with the data source:

void CDatabase::Close()
{
	if (m_Session.m_spOpenRowset!=NULL)
		m_Session.Close();
}

Insert call to this method in CDatabase destructor:

CDatabase::~CDatabase()
{
	Close();
}

Now you class definition should look as follows:

class CDatabase  
{
public:
	CSession& Session()
	{
		return m_Session;
	}

	void Close();
	HRESULT Open(LPCTSTR dataFile);
	CDatabase();
	virtual ~CDatabase();

private:
	CSession m_Session;
};

Methods declarations are already provided.

7. Now let's declare a member of CMyFirst class with type of CDatabase and assign it name m_DB. Right-click CMyFirst class, select Add Member Variable... in menu, type CDatabase as variable type and m_DB as variable name. Give it private access.

Go to the next step.


© 2001-2003 XPerience Technologies. www.xperiencetech.com

Created by chm2web html help conversion utility.