Click here to Skip to main content
11,631,992 members (70,417 online)
Rate this: bad
Please Sign up or sign in to vote.
See more: VC10.0
I am a vc++ developer, and I want to know that, is it possible to connect a database with mfc dialog based application? If it possible
Please give me some references about it, or some example codes of it.

Sibi V J
Posted 4-Jan-11 0:02am
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

Yes, it is possible.
This[^] MSDN article is a good start point for MFC database programming. Smile | :)
Dalek Dave at 4-Jan-11 6:36am
Good Link.
Nuri Ismail at 4-Jan-11 6:38am
Thank you DD. :)
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

Here's a beginners article on the subject: Using the CDatabase class to read an Access databases[^]
SIBI V.J. at 5-Jan-11 3:57am
In this way writing data's to tables is possible?? I already connected database and it now reads values successful.
Rajesh R Subramanian at 6-Jan-11 8:38am
CDatabase allows you to write as well. Why not read the docs?
Dalek Dave at 4-Jan-11 6:36am
Good Link
Rate this: bad
Please Sign up or sign in to vote.

Solution 3

Have a look at

I have an MFC dialog based application but its many years since I wrote the connection part - and I was told that such was the "mixed grill" used by me that it was a miricle that I got a connection.

This was the all important code:
<pre lang="midl">// Connect to the database.
   m_dbHost = _T("localhost");
   m_dbUser = _T("MySampleDB");
   m_dbPassword = _T("MyPassword0;);
   m_dbName = _T("vendatic");
   conn = mysql_init(NULL);
   mysql_real_connect (conn,m_dbHost, m_dbUser, m_dbPassword, m_dbName, 0, NULL, 0 );

I should be able to dig out a little more detail if needed.

Have a look at .NET - If you are just starting out then its the way to go. I reached a point in what I was doing that it was quicker to start over in .NET that continue with MFC. Assemblies are just so much easier than COM objects.
Rate this: bad
Please Sign up or sign in to vote.

Solution 4

Thanks.. The following code got a connection between Ms Access and MFC dialog based application.....But I can't see any methods to write data in to database. Please refer that also....

CDatabase database;
	CString SqlString;
	CString strUname, strPword;
	CString sDriver = _T("MICROSOFT ACCESS DRIVER (*.mdb, *.accdb)");
	CString sDsn;
	CString sFile = _T("D:\\S-talk\\S-talk\\S-talkDB.accdb");
	// You must change above path if it's different
	int iRec = 0; 	
	// Build ODBC connection string
		// Open the database
		// Allocate the recordset
		CRecordset recset( &database );
		// Build the SQL statement
		SqlString =  _T("select Username,Password from Login");
		// Execute the query
		// Reset List control if there is any data
		// Loop through each record
		while( !recset.IsEOF() )
			// Copy each column into a variable
		// Close the database
	CATCH(CDBException, e)
		// If a database exception occured, show error msg
		AfxMessageBox(_T("Database error: ") + e->m_strError);
Rate this: bad
Please Sign up or sign in to vote.

Solution 5

You have to select SDI or MDI application for database handling.
Database handling is not supported by Dialog box application.

=> Includes database header files, link libraries, a record view, and a recordset. (Available only for applications with the Document/view architecture support option selected on the Application Type, MFC Application Wizard page.)
Jochen Arndt at 14-Jan-15 3:16am
This is wrong. There are many different methods to access databases that are not related to the VC application type or even MFC.

And why you are answering a four year old question?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 8,393
1 Sergey Alexandrovich Kryukov 8,044
2 Mika Wendelius 6,260
3 F-ES Sitecore 2,321
4 Suvendu Shekhar Giri 1,970

Advertise | Privacy | Mobile
Web03 | 2.8.150728.1 | Last Updated 14 Jan 2015
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100