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

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
Who is Participating?
lakshman_ceConnect With a Mentor Commented:
#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.

>>#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

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
wwrigh4Author Commented:
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;
wwrigh4Author Commented:
Thanks for your help, lakshman.  I now have it working.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.