Click here to Skip to main content
11,803,861 members (80,485 online)
Rate this: bad
Please Sign up or sign in to vote.
See more: C++ 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?
Padmanabh Kulkarni at 14-Aug-15 2:24am
I thing same. We can not handle DB using DialogBox. because by default it not support DB header files. Some how we can do in Dialog box but its better to do it in SDI/MDI. Please confirm.

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

  Print Answers RSS
0 OriginalGriff 450
1 CPallini 320
2 F-ES Sitecore 240
3 Sergey Alexandrovich Kryukov 234
4 CHill60 220
0 OriginalGriff 3,000
1 Maciej Los 1,910
2 KrunalRohit 1,862
3 CPallini 1,755
4 Richard MacCutchan 1,187

Advertise | Privacy | Mobile
Web03 | 2.8.151002.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