Solved

Establishing a connection to MySQL database

Posted on 2004-09-30
2
237 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

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…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

821 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