Planet MySQL Planet MySQL: Meta Deutsch Español Français Italiano 日本語 Русский Português 中文
MySQL Certification Study: Write a basic .NET application that uses MySQL
+6 Vote Up -0 Vote Down
I've registered for the MySQL 5.6 beta exams, just like many more on Planet MySQL have done.

One of the topics for the MySQL 5.6 Developer exam is: "Write a basic .NET application that uses MySQL".

I have used Connector/Net a few times the last few years, but the last time I looked at some code involving Connector/Net was when I took the MySQL 5 Developer exam, which was long ago.

I'm using Linux on both my laptop and on servers for most projects. So I don't use or see many .NET code.

So I need a short refresh.

To use Connector/Net on Linux you will need to install Mono. The base for .NET is CIL, which exists in at least 4 versions: 1.0, 2.0 and 4.0. To compile C# code with mono you need the command which correspondends with the CIL version you are using.
  • CIL 1.0: mcs
  • CIL 2.0: gmcs
  • CIL 4.0: dmcs
    For Ubuntu you'll need these packages:
    • libmono-system-data4.0-cil
    • libmysql-cil-dev
    • mono-runtime
    • mono-dmcs
    • pkg-config
    So now we should be able to comple some C# code:
    dmcs -r:System.Data.dll $(pkg-config --libs mysql-connector-net) example1.cs


    And run it:
    mono example1.exe

    The Connector/Net Tutorial Intro on mysql.com has some example code.

    I wrote some very very basic C# code based on the tutorial:
    $ mono example1.exe
    Connecting to MySQL...
    5.6.12-log
    Done.

    The code:
    using System;
    using System.Data;

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

    public class Example1
    {
    public static void Main(string[] args)
    {
    string connStr = "server=localhost;user=msandbox;database=test;port=5612;password=msandbox";
    MySqlConnection conn = new MySqlConnection(connStr);
    try
    {
    Console.WriteLine("Connecting to MySQL...");
    conn.Open();

    string sql = "SELECT VERSION()";
    MySqlCommand cmd = new MySqlCommand(sql, conn);
    MySqlDataReader rdr = cmd.ExecuteReader();

    while (rdr.Read())
    {
    Console.WriteLine(rdr[0]);
    }
    }
    catch (Exception ex)
    {
    Console.WriteLine(ex.ToString());
    }
    conn.Close();
    Console.WriteLine("Done.");
    }
    }

    Votes:

    You must be logged in with a MySQL account to vote on Planet MySQL entries. More information on PlanetMySQL voting.

    Planet MySQL © 1995, 2014, Oracle Corporation and/or its affiliates   Legal Policies | Your Privacy Rights | Terms of Use

    Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.