Solved

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

Posted on 2006-06-16
5
973 Views
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>

Compiling...
SQLSample.cpp
.\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
0
Comment
Question by:wwrigh4
  • 2
  • 2
5 Comments
 
LVL 15

Expert Comment

by:lakshman_ce
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
http://msdn.microsoft.com/vstudio/express/visualc/usingpsdk/




0
 
LVL 49

Expert Comment

by:DanRollins
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
    [OK]
3) Select your DSN from the DataSource / ODBC drop-down list.
    [OK]
4) Select a table from the "Select database Table" list
    [OK]
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  
   http://msdn2.microsoft.com/en-us/92bcy0kw(vs.80).aspx

-- Dan
0
 

Author Comment

by:wwrigh4
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<system.dll>
using namespace System;
using namespace std;

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

Accepted Solution

by:
lakshman_ce earned 500 total points
ID: 16985489
change
#import "c:\program files\common files\system\ado\msado15.dll" rename ("EOF","adoEOF") no_namespace
to
#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.

0
 

Author Comment

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

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

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 seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

746 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

13 Experts available now in Live!

Get 1:1 Help Now