[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 243
  • Last Modified:

Establishing a connection to MySQL database

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
KellyJensen
Asked:
KellyJensen
1 Solution
 
drichardsCommented:
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
 
KellyJensenAuthor Commented:
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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now