Click here to Skip to main content
Licence 
First Posted 20 Feb 2001
Views 220,912
Downloads 2,963
Bookmarked 60 times

Using the CDatabase class to read an Access databases

By | 29 Mar 2001 | Article
This is a very simple code snippet that demonstrates how to read a Microsoft Access database file using the CDatabase class
 
Part of The SQL Zone sponsored by
See Also

Sample Image - ReadDB.gif

Introduction

This is a very simple code snippet that demonstrates how to read a Microsoft Access database using the CDatabase class.

The main features it demonstrates are:

  • Retrieving data from Microsoft Access database
  • Connecting without the need for an ODBC data source to be set up.
  • Populate a List view Control with the data
//
// Part of the source code
void CReadDBDlg::OnRead() 
{
	// TODO: Add your control notification handler code here
	CDatabase database;
	CString SqlString;
	CString sCatID, sCategory;
	CString sDriver = "MICROSOFT ACCESS DRIVER (*.mdb)";
	CString sDsn;
	CString sFile = "d:\\works\\ReadDB\\Test.mdb";
	// You must change above path if it's different
	int iRec = 0; 	
	
	// Build ODBC connection string
	sDsn.Format("ODBC;DRIVER={%s};DSN='';DBQ=%s",sDriver,sFile);
	TRY
	{
		// Open the database
		database.Open(NULL,false,false,sDsn);
		
		// Allocate the recordset
		CRecordset recset( &database );

		// Build the SQL statement
		SqlString =  "SELECT CatID, Category "
				"FROM Categories";

		// Execute the query
		recset.Open(CRecordset::forwardOnly,SqlString,CRecordset::readOnly);
		// Reset List control if there is any data
		ResetListControl();
		// populate Grids
		ListView_SetExtendedListViewStyle(m_ListControl,LVS_EX_GRIDLINES);
 
		// Column width and heading
		m_ListControl.InsertColumn(0,"Category Id",LVCFMT_LEFT,-1,0);
		m_ListControl.InsertColumn(1,"Category",LVCFMT_LEFT,-1,1);
		m_ListControl.SetColumnWidth(0, 120);
		m_ListControl.SetColumnWidth(1, 200);

		// Loop through each record
		while( !recset.IsEOF() )
		{
			// Copy each column into a variable
			recset.GetFieldValue("CatID",sCatID);
			recset.GetFieldValue("Category",sCategory);

			// Insert values into the list control
			iRec = m_ListControl.InsertItem(0,sCatID,0);
			m_ListControl.SetItemText(0,1,sCategory);

			// goto next record
			recset.MoveNext();
		}
		// Close the database
		database.Close();
	}
	CATCH(CDBException, e)
	{
		// If a database exception occured, show error msg
		AfxMessageBox("Database error: "+e->m_strError);
	}
	END_CATCH;
}
	
// Reset List control
void CReadDBDlg::ResetListControl()
{
	m_ListControl.DeleteAllItems();
	int iNbrOfColumns;
	CHeaderCtrl* pHeader = (CHeaderCtrl*)m_ListControl.GetDlgItem(0);
	if (pHeader)
	{
		iNbrOfColumns = pHeader->GetItemCount();
	}
	for (int i = iNbrOfColumns; i >= 0; i--)
	{
		m_ListControl.DeleteColumn(i);
	}
}

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

About the Author

zhaque

Web Developer

Australia Australia

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralMy vote of 5 PingroupMahdi Nejadsahebi19:15 27 Apr '12  
GeneralError messages in running Using the CDatabase class to read an Access databases Pinmembershiphy20:03 21 Mar '10  
GeneralRe: Too much in Try/Catch [modified] Pinmembershiphy11:15 19 Mar '10  
GeneralToo much in Try/Catch PinmemberWilliam Winner9:49 19 Mar '10  
GeneralHaving problems showing data in my Access Database in the List Control Pinmembershiphy9:18 19 Mar '10  
GeneralDatabase Error [modified] PinmemberMember 379530419:24 29 Jun '08  
GeneralRe: Database Error Pinmemberzhaque14:00 3 Feb '10  
GeneralDataBase Error PinmemberMember 379530419:22 29 Jun '08  
GeneralThank you very much Pinmemberjellow8:49 27 Feb '08  
QuestionHow to use Other databases Like "SQL"" Pinmemberrag84dec20:27 21 May '07  
QuestionRe: Multiple table help, please? Pinmembermla1546:08 27 Feb '07  
QuestionAccess file creation PinmemberArum M S18:28 17 Jan '07  
QuestionIn modal dglboxes? Pinmemberswapna_signsin0:22 11 May '06  
Generalc/c++ with sql/access database PinsussAnonymous22:35 5 Oct '05  
Generalunresolved external symbol PinmemberWinAPILearner11:44 31 Oct '04  
GeneralRe: unresolved external symbol PinsussAnonymous10:46 18 Feb '05  
GeneralRe: unresolved external symbol Pinmemberzhaque7:25 31 Oct '05  
GeneralBound error Pinmemberviva dotnet11:24 18 Oct '04  
AnswerRe: Bound error Pinmemberliuxiaop2:43 12 Feb '07  
Generaldatabase + listcontrol Pinmembersakesh20:50 16 Sep '04  
QuestionUnicode? PinmemberBadaa2:19 25 Apr '04  
AnswerRe: Unicode? Pinmembernvoigt23:07 27 Jul '04  
GeneralPassword PinsussAnonymous18:38 24 Apr '04  
GeneralUnresolved External Link Errors Pinmemberpaullo2:14 20 Apr '04  
GeneralWHERE Date>=#8/01/2004# Pinmemberalexcoder23:25 7 Jan '04  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web01 | 2.5.120517.1 | Last Updated 30 Mar 2001
Article Copyright 2001 by zhaque
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid