Click here to Skip to main content
15,893,622 members
Articles / Desktop Programming / WTL

Using Blobs with WTL OLE DB Database Applications

Rate me:
Please Sign up or sign in to vote.
4.80/5 (4 votes)
6 Sep 20026 min read 126.8K   6.1K   32  
Describes an extension to DDX that can read and write blobs using OLE DB providers. Includes sample WTL OLE DB database projects for SQL Server "Pubs" and MS Access "Northwind"
// Categories.H : Declaration of the CCategories class

#ifndef __CATEGORIES_H_
#define __CATEGORIES_H_

#ifndef __BLOBDDX_H__
	#error pubinfo.h requires blobddx.h to be included first
#endif

class CCategoriesAccessor
{
public:
	LONG m_CategoryID;	// Number automatically assigned to a new category.
	TCHAR m_CategoryName[16];	// Name of food category.
	TCHAR m_Description[1024];	// Name of food category.
	ISequentialStream* m_Picture;	// A picture representing the food category.

	// for BLOB_ENTRY_LENGTH_STATUS macro
	ULONG m_PictureLength;
	ULONG m_PictureStatus;

BEGIN_COLUMN_MAP(CCategoriesAccessor)
	COLUMN_ENTRY(1, m_CategoryID)
	COLUMN_ENTRY(2, m_CategoryName)
	COLUMN_ENTRY(3, m_Description)
	BLOB_ENTRY_LENGTH_STATUS(4, IID_ISequentialStream, STGM_READ, m_Picture, m_PictureLength, m_PictureStatus)
END_COLUMN_MAP()

DEFINE_COMMAND(CCategoriesAccessor, _T(" \
	SELECT \
		CategoryID, \
		CategoryName, \
		Description, \
		Picture  \
		FROM Categories"))

	// You may wish to call this function if you are inserting a record and wish to
	// initialize all the fields, if you are not going to explicitly set all of them.
	void ClearRecord()
	{
		memset(this, 0, sizeof(*this));
	}
};

class CCategories : public CCommand<CAccessor<CCategoriesAccessor> >
{
public:
	HWND m_hWndParent; // for file open dialog

	HRESULT Open()
	{
		HRESULT		hr;

		hr = OpenDataSource();
		if (FAILED(hr))
			return hr;

		return OpenRowset();
	}
	HRESULT OpenDataSource()
	{
		HRESULT		hr;
		CDataSource db;
		CDBPropSet	dbinit(DBPROPSET_DBINIT);

		dbinit.AddProperty(DBPROP_AUTH_CACHE_AUTHINFO, true);
		dbinit.AddProperty(DBPROP_AUTH_ENCRYPT_PASSWORD, false);
		dbinit.AddProperty(DBPROP_AUTH_MASK_PASSWORD, false);
		dbinit.AddProperty(DBPROP_AUTH_PASSWORD, OLESTR(""));
		dbinit.AddProperty(DBPROP_AUTH_USERID, OLESTR("Admin"));

		// file open dialog
		LPOLESTR osFileName = NULL;
		LPCTSTR lpcstrFileFilters = _T("Access databases (*.mdb)\0*.mdb\0All Files (*.*)\0*.*\0");
		CFileDialog dlg(TRUE, _T("mdb"), _T("Northwind.mdb"), OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, lpcstrFileFilters, m_hWndParent);
		if (dlg.DoModal() == IDOK) osFileName = CComBSTR(dlg.m_szFileName);
		dbinit.AddProperty(DBPROP_INIT_DATASOURCE, osFileName);

		dbinit.AddProperty(DBPROP_INIT_MODE, (long)16);
		dbinit.AddProperty(DBPROP_INIT_PROMPT, (short)4);
		dbinit.AddProperty(DBPROP_INIT_PROVIDERSTRING, OLESTR(""));
		dbinit.AddProperty(DBPROP_INIT_LCID, (long)1033);
		hr = db.Open(_T("Microsoft.Jet.OLEDB.4.0"), &dbinit);
		if (FAILED(hr))
			return hr;

		return m_session.Open(db);
	}
	HRESULT OpenRowset()
	{
		// Set properties for open
		CDBPropSet	propset(DBPROPSET_ROWSET);
		propset.AddProperty(DBPROP_IRowsetChange, true);
		propset.AddProperty(DBPROP_UPDATABILITY, DBPROPVAL_UP_CHANGE | DBPROPVAL_UP_INSERT | DBPROPVAL_UP_DELETE);

		return CCommand<CAccessor<CCategoriesAccessor> >::Open(m_session, NULL, &propset);
	}
	CSession	m_session;
};

#endif // __CATEGORIES_H_

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Founder Choycer
United States United States
Ed has over 40 years experience in computer technology and a bachelor's degree in Business Administration. He's currently a marketing technology consultant. During his career, he's led software development departments and created software still in use in the communications and healthcare industries. Ed is a veteran of the United States Army. He lives in Arizona in the United States.

Find Ed on Linkedin.

This material is copyright 2019 by Ed Gadziemski. Unauthorized use is strictly prohibited. All rights reserved.

Comments and Discussions