Solved

mfc Sql server or mysql

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
spx for moving values to new table 5 59
SQL Agent Timeout 5 38
Mysqli Query 5 40
mysql sql statement - SQL INSERT INTO SELECT 11 46
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
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.
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

708 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

16 Experts available now in Live!

Get 1:1 Help Now