Solved

mfc Sql server or mysql

Posted on 2009-04-05
4
607 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

Suggested Solutions

Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL (http://www.experts-exchange.com/articles/201/Handling-Date-and-Time-in-PHP-and-MySQL.html) several years ago, it seemed like now was a good time to updat…
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
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.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

803 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