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!