Mono Develop – Use .net MySql connector


source-codeA 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.

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

[sourcecode language=”csharp”]
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");
}
}
}
[/sourcecode]

Happy programming!

Have any Question or Comment?

Leave a Reply