Solved

Establishing a connection to MySQL database

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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

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…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

785 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