?
Solved

mfc Sql server or mysql

Posted on 2009-04-05
4
Medium Priority
?
612 Views
Last Modified: 2013-11-20
Hi
I would like to know how to connect to mysql or sql server database from visual C++ MFC "Dialog Based " application, please provide an example if you can
thanks
0
Comment
Question by:klay8
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 19

Assisted Solution

by:alb66
alb66 earned 300 total points
ID: 24074816
May be this can help you:

"MFC and MySQL"
http://www.codeproject.com/KB/database/dlgmysql.aspx
0
 
LVL 39

Assisted Solution

by:itsmeandnobodyelse
itsmeandnobodyelse earned 300 total points
ID: 24090716
If mysql you would need an ODBC driver to install (it should be easy to get one on the web). For SQLServer there is already an ODBC driver installed with VisualStudio 2005.

Add a datasource for your DBMS by calling odbcad32.exe from Run in start menu  - or choose 'System Settings - Administration - Data sources (ODBC) ' from menu.

A data source is a name pointing to a DBMS. You can choose between user or system data sources. I would use system datasource or the access to the database is restricted for your login user.

Select the appropriate ODBC driver from list (that's why you need a ODBC driver for MySQL if not already installed) and add a new datasource.

Then go to your MFC project in Visual Studio and add a 'new class ...' via Project menu. You can choose ODBC class and connect to your DBMS by selecting the datasource.

0
 
LVL 2

Accepted Solution

by:
graber earned 1400 total points
ID: 24132918
Here is what I have.

1.) you will have to create a database.  I've used Access but that is all that has been available to me.

2.) inside the control panel there is an icon odbc data source.  When you open this there is a tab for user DSN.  Press the add button.  This will allow you to select the database that you created in step 1.

3.) here is a short program for accessing the database.  Here the example was for a simple database containing a last name and first name.
#include <afx.h>
#include <afxdb.h>
#include <conio.h>

void main()
{
  CDatabase *pMyDbase=new CDatabase;
  CRecordset *pMyRecord;
  CString sSQL="SELECT Last, First FROM Person ";
  CString sSQL1;
  CString sFirst;
  CString sSecond;
  CDBVariant dbVFirst;
  CDBVariant dbVSecond;

  sSQL1.Format("WHERE Last = %s;", "Flinstone");
  try
    {
      pMyDbase->OpenEx(_T("DSN=Database"),CDatabase::openReadOnly);
      pMyRecord= new CRecordset(pMyDbase);
      pMyRecord->Open(CRecordset::snapshot,sSQL+sSQL1);
      while(!pMyRecord->IsEOF())
      {
        pMyRecord->GetFieldValue((short)0, sFirst);
        pMyRecord->GetFieldValue(1, dbVSecond);
        pMyRecord->MoveNext();
      }
      pMyDbase->Close();
      getch();
    }
    catch(...)
    {
      pMyDbase->Close();
    }
}
The short on the zero of first element is required.  Why I don't know.  It came down from the microsoft engineers.

Hope that is of some help.  Good luck and good coding.
Gregg
0
 
LVL 2

Expert Comment

by:graber
ID: 24133038
you will have to update each of the editboxes programatically. The basic steps for opening an ODBC connection is the same thou.  I did this several years back.
Gregg
0

Featured Post

Get proactive database performance tuning online

At Percona’s web store you can order full Percona Database Performance Audit in minutes. Find out the health of your database, and how to improve it. Pay online with a credit card. Improve your database performance now!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

By, Vadim Tkachenko. In this article we’ll look at ClickHouse on its one year anniversary.
Backups and Disaster RecoveryIn this post, we’ll look at strategies for backups and disaster recovery.
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

800 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question