Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
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.

Thanks
Sibi V J
Posted

Yes, it is possible.
This[^] MSDN article is a good start point for MFC database programming. :)
 
Share this answer
 
Comments
Dalek Dave 4-Jan-11 6:36am    
Good Link.
Nuri Ismail 4-Jan-11 6:38am    
Thank you DD. :)
Here's a beginners article on the subject: Using the CDatabase class to read an Access databases[^]
 
Share this answer
 
Comments
Dalek Dave 4-Jan-11 6:36am    
Good Link
SIBI V.J. 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 6-Jan-11 8:38am    
CDatabase allows you to write as well. Why not read the docs?
Have a look at http://www.codeproject.com/KB/cpp/sdba.aspx.

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.
 
Share this answer
 
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
	sDsn.Format(_T("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 =  _T("select Username,Password from Login");

		// Execute the query
		recset.Open(CRecordset::forwardOnly,SqlString,CRecordset::readOnly);
		// Reset List control if there is any data
		
		// Loop through each record
		while( !recset.IsEOF() )
		{
			// Copy each column into a variable
			recset.GetFieldValue(_T("Username"),strUname);
			recset.GetFieldValue(_T("Password"),strPword);
			recset.MoveNext();
		}
		// Close the database
		database.Close();
	}
	CATCH(CDBException, e)
	{
		// If a database exception occured, show error msg
		AfxMessageBox(_T("Database error: ") + e->m_strError);
	}
	END_CATCH;
 
Share this answer
 
v3
You have to select SDI or MDI application for database handling.
Database handling is not supported by Dialog box application.

Why?
=> 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.)
 
Share this answer
 
Comments
Jochen Arndt 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 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)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900