Click here to Skip to main content
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 1: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

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

  Print Answers RSS
0 OriginalGriff 390
1 Sergey Alexandrovich Kryukov 329
2 Afzaal Ahmad Zeeshan 244
3 BillWoodruff 210
4 Maciej Los 192
0 OriginalGriff 5,560
1 DamithSL 4,476
2 Maciej Los 3,942
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,175


Advertise | Privacy | Mobile
Web04 | 2.8.141216.1 | Last Updated 5 Jan 2011
Copyright © CodeProject, 1999-2014
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