MySql in C#

First download the Mysql ODBC driver from the download page.

Then download the .net connector, at this writing it was Mysql Connector/Net 5.2

You can choose to download only the binaries, extract and add /bin/MySql.Data.dll to your project. A help file for the classes is found at /Docs/MySql.Data.chm of the download.

With the dll added to the project, it’s very simple to connect to a MySql database as the snippet below.

using MySql.Data.MySqlClient;

namespace test
    class testit
        private MySqlConnection mConn = null;
        private const string msConnStr = “” +

        public void test()
                // Setup and open acceess to the database
                mConn = new MySqlConnection(msConnStr);
                // simple count select
                MySqlCommand command = new MySqlCommand(“select count(*) from cars”, mConn);
                long ret = (long)command.ExecuteScalar();
                // how many rows in the table
                Console.WriteLine(“cars count=” + ret );
            catch (Exception ex)
                Console.WriteLine(“err=” + ex);
                if( mConn != null ) mConn.Close();

Leave a Reply