Solved

Establishing a connection to MySQL database

Posted on 2004-09-30
2
235 Views
Last Modified: 2010-04-24
I am using MySQL, Microsoft NET Framework 1.1, and trying to establish a connection to my
database using C++ .NET. The code compiles, but I am unable to establish a connection. I get the Error
[IM002] - “Data Source name not found and no default driver specified.” I have installed the
MySQL ODBC 3.51 Driver, created the DSN to my database “nutrition”, and installed MDAC.
What is wrong with my Connection string?

#include "stdafx.h"
#using <mscorlib.dll>
#using <System.dll>
#using <System.Data.dll>
#using <System.Xml.dll>

using namespace System;
using namespace System::Data;
using namespace System::Data::Odbc;
using namespace System::Xml;

Int32 main(void)
{
      OdbcConnection *connection = new OdbcConnection();

      connection->ConnectionString =
            S"DRIVER= {MySQL ODCB 3.51 Driver};"
            S"Data Source = (local);"
            S"Initial Catalog = nutrition;"
            S"Database=nutrition;"
            S"User ID = root;"
            S"Password=password;";
      try
      {
            connection->Open();
            Console::WriteLine(S"We got a connection!");
      }
      catch (OdbcException *e)
      {
            Console::WriteLine(S"No connection the following error occured: {0}",
                  e->Message);
      }
      __finally
      {
            connection->Close();
            Console::WriteLine(S"The connection to the database has been closed");
      }
      return 0;
}
0
Comment
Question by:KellyJensen
2 Comments
 
LVL 19

Accepted Solution

by:
drichards earned 150 total points
ID: 12196821
I don't remember whether you use PASSWORD or PWD, but I believe the rest is correct.

     connection->ConnectionString =
          S"DRIVER= {MySQL ODCB 3.51 Driver};"
          S"SERVER=localhost;"
          S"DATABASE=nutrition;"
          S"UID = root;"
          S"PWD=password;"; // Can't remember whether it's PASSWORD or PWD
0
 

Author Comment

by:KellyJensen
ID: 12197386
I have solved my own question. As it turns out, I did not need the ConnectionString with all the parameters listed as above. Since I had a DSN(Data Source Name) created, the code was actually much simpler. The above is a DSN-less connection, since I already had a DSN, I only needed only to put the DSN in my code like so:

#include "stdafx.h"
#include <iostream>

#using <mscorlib.dll>
#using <System.dll>
#using <System.Data.dll>
#using <System.Xml.dll>

using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Data;
using namespace System::Data::Odbc;
using namespace System::Xml;

int _tmain()
{
     OdbcConnection *connection = new OdbcConnection("DSN=nutrition");

     try
     {
          connection->Open();
          Console::WriteLine(S"We got a connection!");
     }
     catch (OdbcException *e)
     {
          Console::WriteLine(S"No connection the following error occured: {0}",
               e->Message);
     }
     __finally
     {
          connection->Close();
          Console::WriteLine(S"The connection to the database has been closed");
     }
     return 0;
}

Simple huh!!!! Now, since I don't want anyone to suffer as I have suffered, here is how you create your DSN. (Do this even before you start coding.

1) Choose "Tools->Connect to Database" from the .NET Menu bar

2) In the "Data Link Properties" pop up

         a) Under the "Provider" tab choose "Microsoft OLE DB Provider for ODBC Drivers".

         b) Under the "Connection" tab, put your database name in "Use data source name", your user name in "user name", your                                    password under "Password", and your database name again under "Enter initial catalog to use."

         c) Hit "Test Connection" button

         d) Hit "OK"

Ooops. I realized that I'm not supposed to post the same question in two different sections of this forum. My bad. I won't do it again. I would have deleted this question, but someone had already answered. I'm giving points to "drichards", because he was the one of two people to help me. Thanks.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
need some help with Editbox 11 105
Need some help with open a dialog 5 210
3D Surface or Implicit Fairing Visual Studio 4 111
L prefix in C++ 8 103
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…
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

863 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

20 Experts available now in Live!

Get 1:1 Help Now