Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

mfc Sql server or mysql

Posted on 2009-04-05
4
Medium Priority
?
614 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
In this blog, we’ll look at how improvements to Percona XtraDB Cluster improved IST performance.
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 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…

610 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