Click here to Skip to main content
11,721,385 members (84,488 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: VC10.0
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 4-Jan-11 0:02am
Rate this: bad
good
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 | :)
  Permalink  
Comments
Dalek Dave at 4-Jan-11 6:36am
   
Good Link.
Nuri Ismail at 4-Jan-11 6:38am
   
Thank you DD. :)
Rate this: bad
good
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[^]
  Permalink  
Comments
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
good
Please Sign up or sign in to vote.

Solution 3

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.
  Permalink  
Rate this: bad
good
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
	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;
  Permalink  
v3
Rate this: bad
good
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.

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.)
  Permalink  
Comments
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 Sergey Alexandrovich Kryukov 510
1 OriginalGriff 249
2 Mika Wendelius 170
3 F-ES Sitecore 80
4 cyycoish 70
0 Sergey Alexandrovich Kryukov 2,209
1 OriginalGriff 969
2 F-ES Sitecore 660
3 Maciej Los 609
4 Richard MacCutchan 560


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