Partager via


Azure: Using C# to connect to a locally hosted MySQL Database

Next Blog

With a little regret, I think that the C#, .NET, Entity Framework, MySQL object and so forth need to be discussed.  When I attempted to implement a connection to a MySQL I found out that the path was not all that obvious to me.  In case you are like me, this article might be of interest.  If you have access to the Azure Dreamspark subscription, at this time, 6/19/2015, then your database is MySQL.  To get started using the Azure Dreamspark subscription, you will need to be able to work with the MySQL.

My approach to this would be to get the MySQL working when it is directly connected to the Visual Studio product without Azure.  Then figure out how to connect to Azure.  MySQL and SQL Express both can be used without using Azure, but your update process is different.  This is the first in a number of MySQL blogs I am working on.

Goal:

  • Connect from Visual Studio 2013 Ultimate with a Sample MySQL Database named: Actors

Sources:

Discussion:

1. You will need to add references to your project.  You could use Nuget, but the reference process is much quicker, make sure the checkboxes have a check before clicking on “OK”

image

2. Now go to the link:

  • 4.2 Making a Connection
  • This link guides you through making a connection to the appropriate MySQL databases.
    • Link and information valid on VS 2013 Ultimate on 6/19/2015

3. Now go to the link: 5.1.1 The MySqlConnection Object, this will give you code, that might not work if you did what I did.  What did I do?  I opened a Console C# App, which has a requirement for the main function.  So my code looked like the following, in the line: “static void Main(string[] args)” was changed in the example code to:”public static void Main()”.  In the console app it won’t work as written, but just change the Main function.

using System;
using System.Data;
using MySql.Data;
using MySql.Data.MySqlClient;

namespace Connect2MySQL
{
    class Program
    {
        static void Main(string[] args)
        {
            string connStr =
                "server=localhost;user=root;database=sakila;port=3306;password=xxxxxxxxx;";
            MySqlConnection conn = new MySqlConnection(connStr);
        try
        {
            Console.WriteLine("Connecting to MySQL...");
            conn.Open();
            // Perform database operations
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.ToString());
        }
        conn.Close();
        Console.WriteLine("Done.");
        } 
  }
}

4. Once you have this working correctly, you will see the following:

image

5. If you get the following, it means your connection isn’t working, check your password and connection string:

image

That’s it.

Conclusion

This is a fairly simple blog that doesn’t deviate from the MySQL sample.  Using it will show you how to use the MySQL site.  It is likely you used this site during college or elsewhere, but if you haven’t, then it will prove that your set-up is correct.

That is all it does: Makes sure you have MySQL set up correctly.  Using Visual Studio, C# and MySQL.  It also showed you how to set the references in case you are use to using Nuget, which should work as well.