Mono Develop – Use .net MySql connector

source-code

A short example on how to use the .net MySql connector with Mono Develop.
First you need to make it available to your project.
Go to Project -> Edit References
Select the .Net Assembly tab. Then Navigate to your folder containing Mysql.Data.dll and select the file, and press ok.

If you are using Linux and need to install the MySql connector, check out my article on how to install mysql connector. There are a few things you need to be aware of.

Here is a short example program on how to insert a record into a test MySql database.

using System;
using MySql.Data.MySqlClient;

namespace testmysql
{
  class MainClass
  {
    public static void Main (string[] args)
    {
      // First a few variables, Assuming MySQL server is at 192.168.1.15 and default port 3306
      string cs = @"server=192.168.1.15;port=3306;userid=testuser;password=testpass;database=testdb";
      MySqlConnection myCon = null;
      MySqlCommand myCmd = null;
      //Greating
      Console.WriteLine ("Welcome to Test MySQL....");
      // create mysql connector, and connect
      Console.WriteLine("creating mysql connector...");
      myCon = new MySqlConnection(cs);
      myCon.Open();
      // Create a sql string with sample data, inserted to testtable
      string strQuery = "INSERT INTO testdb.testtable(testdata) values ('Sample Data #1');";
      // Execute
      myCmd = new MySqlCommand(strQuery, myCon);
      myCmd.ExecuteScalar();
      // Close connection
      myCon.Close();
      Console.WriteLine("Data inserted to MySQL database successfully");
    }
  }
}

Happy programming!

About Author

Related Posts

mysql double vs float

MySQL: Double vs Float

When it comes to mysql double vs float to storing numbers with decimal places in a database, two of the most commonly used data types in MySQL…

C# Reference Types

Understanding C# Reference Types

One of the key features of C# is its support for C# reference types, which allow developers to create complex, object-oriented applications. In this blog post, we…

c# value types

Understanding C# Value Types

C# is a powerful programming language that is widely used for developing a wide range of applications. One of the key features of C# is its support…

C# check if server is online

C# check if server is online directly from your code. Check servers or services like web servers, database servers like MySQL and MongoDB. You can probably check…

C# Convert Int to Char

C# convert int to char google search is giving some result which I think is not directly what some people want. Most results are giving instructions on…

c# bash script

C# Bash Script Made Easy

There are many reasons why it could be handy to run a bash script from a C# application. In 2014, before I changed to a Mac as…

Leave a Reply