Solved

Establishing a connection to MySQL database

Posted on 2004-09-30
2
234 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
Comment Utility
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
Comment Utility
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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

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…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

772 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

12 Experts available now in Live!

Get 1:1 Help Now