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


Accessing MySql using ODBC from MS Visual C++ 2005 Express Edition

Posted on 2006-06-16
Medium Priority
Last Modified: 2008-02-26
I'm using the free Microsoft Visual C++ 2005 Express Edition and would like to use ODBC to access data from MySql.  I tried using some sample code I found online but am having trouble building the application.  I'm new to MS Visual C++ environment so I'm not sure I've included all the necessary header files, etc...  The following code results in the following error.

#import "msado15.dll" rename ("EOF","adoEOF") no_namespace
#include "stdafx.h

#include <comdef.h>
#include <stdio.h>

.\SQLSample.cpp(5) : error C2001: newline in constant
C:\Program Files\Microsoft Visual Studio 8\VC\include\comdef.h(24) : fatal error C1083: Cannot open include file: 'ole2.h': No such file or directory
Question by:wwrigh4
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
  • 2
LVL 15

Expert Comment

ID: 16934476
>>#include "stdafx.h

missing double quote at the end

>>#include <comdef.h>

Did you install Platform SDK

Refer to this link for VS Express install instructions

LVL 49

Expert Comment

ID: 16937087
Perhaps the easiest way to access any database is to use the MFC objects which use an ODBC interface.  MySQL installs ODBC support, so you should be able to use the
      Windows Control Panel / Administrative Tools / Data SOurces (ODBC)
to create a "User DSN" to access any MySQL table.

Having done that, you can now add this line to your StdAfx.h file

   #include <afxdb.h>  // MFC ODBC database classes

Now you can use the Classwizard to create easy-to-use objects that correspond to the tables in your database:
1)  Press Ctrl+W to bring up the ClassWizard
2) [Add Class...] > New...  
    Name: CRsMyTable
    Baseclass: CRecordset
3) Select your DSN from the DataSource / ODBC drop-down list.
4) Select a table from the "Select database Table" list
5) (back in the ClassWozard) Click the Member Variables tab.
    Note that the Wizard has created an object in which there is a member variable that corresponds to each field in that table.

Now in your program you can do (for instance)

#include "RsMyTable/h"

    CRsMyTable crs;
    crs.Open();   // access the table; read first record
    MessageBox( crs.m_LastName, "The last name is:" );

And you can add new records, delete records, modify existing records, cycle through the entire recordset to (for instance) populate a list...
   MFC Library Reference  / CRecordset Class  

-- Dan

Author Comment

ID: 16972356
I have now installed platform SDK and get past the  "ole2.h - file not found error'  
I now get the error that ADODB is not a class. I thought the import command would take care of that.
As for using MFC, C++ Express Edition does not include MFC.  I agree it does seem easier.

 error C2653: 'ADODB' : is not a class or namespace name
Build log was saved at "file://c:\Documents and Settings\Administrator\My Documents\Visual Studio 2005\Projects\ADO_test\ADO_test\Debug\BuildLog.htm"

// ADO_test.cpp : Defines the entry point for the console application.

#include "stdafx.h"

#import "c:\program files\common files\system\ado\msado15.dll" rename ("EOF","adoEOF") no_namespace

#include <stdio.h>
#include <iostream>
#include <comdef.h>
using namespace System;
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
      ADODB::_ConnectionPtr writeCnx;
    Console::WriteLine(L"Hello World");
    return 0;
LVL 15

Accepted Solution

lakshman_ce earned 1500 total points
ID: 16985489
#import "c:\program files\common files\system\ado\msado15.dll" rename ("EOF","adoEOF") no_namespace
#import "c:\program files\common files\system\ado\msado15.dll" rename ("EOF","adoEOF")

The type-library contents in the #import header file are normally defined in a namespace. The namespace name is specified in the library statement of the original IDL file. If the no_namespace attribute is specified, then this namespace is not generated by the compiler.


Author Comment

ID: 16993701
Thanks for your help, lakshman.  I now have it working.

Featured Post

Tech or Treat! - Giveaway

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

Question has a verified solution.

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

The following diagram presents a diamond class hierarchy: As depicted, diamond inheritance denotes when two classes (e.g., CDerived1 and CDerived2), separately extending a common base class (e.g., CBase), are sub classed simultaneously by a fourt…
In Easy String Encryption Using CryptoAPI in C++ (http://www.experts-exchange.com/viewArticle.jsp?aid=1193) I described how to encrypt text and recommended that the encrypted text be stored as a series of hexadecimal digits -- because cyphertext may…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Screencast - Getting to Know the Pipeline

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