Thursday, September 16, 2010

Setting Up an Application for MySql NET Connector

We started out this project working with the C++ connector but due to some character set issues I've switched to the .NET connector. Here's how to do it.

What this covers:
Getting the project settings in an existing MFC project right to allow for .NET Connector to work


You will need:
Visual Studio (I'm working from 2005)
MySQL Connector/Net: my computer has the latest version, but you should probably try versions back if yours has problems installing
Optional: the NETDatabase class that I wrote. Otherwise you can add the appropriate headers and call the Connector functions as you wish.

How to do it:

1) Open up a project in Visual Studio. I'm going to open up a MFC project that already exists so that I don't have to go through the steps of setting that up again.

2) Go to Project Properties > Configuration Properties. Look for "Common Language Runtime support", and change that option to "Common Language Runtime Support (/clr)".
3) Click OK or Apply. Your project might look like it's frozen. Don't worry, it's just updating Intellisense and other things to accommodate for the new setting.

2) Go to Project Properties> Common Properties. Click "Add New Reference..."

It might take a while for the next dialog box to come up. Be patient.

3) There are three references you will need to add. All of them are under the .NET tab, which should be what comes up automatically.:

System
System.Data
MySQL.Data

It doesn't matter which order you pick them. Just add them one by one.

Now you're good to go! When you make your function calls to the Connector you'll just have to tell it what namespace it is in.

All the examples are for C# or Visual Basic but the function calls have the same names and namespaces so you should be able to figure it out. Alternatively, you can use the class I wrote, which is a wrapper for the most common functions that we use.

Because this is a .NET component, it has to be written in managed C++, which is kind of a pain when you're mixing with unmanaged code. I've been using trial and error to figure it out and so far it's worked okay. There are books and articles you can read about that online.

No comments:

Post a Comment