Solved

mfc Sql server or mysql

Posted on 2009-04-05
4
606 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
  • 2
4 Comments
 
LVL 19

Assisted Solution

by:alb66
alb66 earned 75 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 75 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 350 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
In SQL Server, when rows are selected from a table, does it retrieve data in the order in which it is inserted?  Many believe this is the case. Let us try to examine for ourselves with an example. To get started, use the following script, wh…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Delivering innovative fully-managed cloud services for mission-critical applications requires expertise in multiple areas plus vision and commitment. Meet a few of the people behind the quality services of Concerto.

932 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now