Skip to content

Instantly share code, notes, and snippets.

@steenzout
Last active January 3, 2016 04:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save steenzout/8413214 to your computer and use it in GitHub Desktop.
Save steenzout/8413214 to your computer and use it in GitHub Desktop.
How to perform Mono installation and to test C# connectivity to MySQL on a Mac
Copyright 2013-2014 steenzout

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
$ sudo port install mono

# download the MySQL .NET connector from the MySQL website
$ cd ~/Downloads
$ mkdir mysql
$ mv mysql-connector-net-6.8.3-noinstall.zip mysql
$ cd mysql
$ unzip mysql-connector-net-6.8.3-noinstall.zip

$ cd v2.0
$ sudo gacutil /i /package 2.0 MySql.Data.dll 
Package exported to: /opt/local/lib/mono/2.0/MySql.Data.dll -> ../gac/MySql.Data/6.8.3.0__c5687fc88969c44d/MySql.Data.dll
Installed MySql.Data.dll into the gac (/opt/local/lib/mono/gac)

$ cd v4.0
$ sudo gacutil /i /package 4.0 MySql.Data.dll 
Package exported to: /opt/local/lib/mono/4.0/MySql.Data.dll -> ../gac/MySql.Data/6.8.3.0__c5687fc88969c44d/MySql.Data.dll
Installed MySql.Data.dll into the gac (/opt/local/lib/mono/gac)

$ cd v4.5
$ sudo gacutil /i /package 4.5 MySql.Data.dll
Package exported to: /opt/local/lib/mono/4.5/MySql.Data.dll -> ../gac/MySql.Data/6.8.3.0__c5687fc88969c44d/MySql.Data.dll
Installed MySql.Data.dll into the gac (/opt/local/lib/mono/gac)

$ gacutil -l|grep MySql
MySql.Data, Version=6.8.3.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d

$ vim test.cs
using MySql.Data; 
using MySql.Data.MySqlClient;
using System;

namespace Dela.Mono.Examples
{
   public class HelloWorld
   {
      public static void Main(string[] args)
      {
         Console.WriteLine("Hello World");
         string connectionString = "Server=localhost;User ID=usernamehere;Password=passwordhere;Database=ir_mds;Port=3306;Pooling=false";
         MySqlConnection connection = new MySqlConnection( connectionString );

         try
         {
             Console.WriteLine( "Trying to open database connection ..." );
             connection.Open();
          }
          catch(Exception ex)
          {
              Console.WriteLine(ex.ToString());
          }

          connection.Close();
      }
   } 
}
$ dmcs testmy.cs -r:System.Data.dll -r:/opt/local/lib/mono/4.0/MySql.Data.dll
# or
$ dmcs testmy.cs -r:System.Data.dll -r:/opt/local/lib/mono/4.5/MySql.Data.dll

$ mono testmy.exe
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment