Accessing MySQL database using c# on unity? Accessing MySQL database using c# on unity? mysql mysql

Accessing MySQL database using c# on unity?


You will need to get the C# MySQL driver: http://dev.mysql.com/downloads/connector/net/

And then you will need to follow the MySQL manual for setting it up and using it. It's a standard ADO.NET driver, so you should be able to follow most any C# SQL tutorials out there for additional help.


Unity3D uses stripped Mono.net which doesn't support database directly. You can write your own DLL in C# .NET which does data access for you. Then add it in your Unity3D project.

While you are writing your own DLL, make sure when you bring that DLL to your project, you also copy referenced DLL in the same folder. Best approach would be to use Mono.NET.


I have faced the same problem yesterday and I've found a satisfying solution that works both on PC and Android.

1: Download .DLL file from here matching your Visual studio project target .NET version (for me 3.5, version 6.9.8.0 worked just fine). If you download a wrong version you will get an error in Unity editor. links to download the file: https://www.dllme.com/dll/files/mysql_data_dll.html or this one: https://downloads.mysql.com/archives/c-net/

2: Unpack the .DLL file and include it in the project (put it anywhere inside of the Assets folder).

3: Program your connection to the database ;) here is a short example:

 using System; using System.Data; using MySql.Data; using MySql.Data.MySqlClient; public class Tutorial4 {     public static void Main()     {         string connStr = "server=localhost;user=root;database=world;port=3306;password=******";         MySqlConnection conn = new MySqlConnection(connStr);         try         {             Console.WriteLine("Connecting to MySQL...");             conn.Open();             string sql = "SELECT COUNT(*) FROM Country";             MySqlCommand cmd = new MySqlCommand(sql, conn);             object result = cmd.ExecuteScalar();             if (result != null)             {                 int r = Convert.ToInt32(result);                 Console.WriteLine("Number of countries in the world database is: " + r);             }         }         catch (Exception ex)         {             Console.WriteLine(ex.ToString());         }         conn.Close();         Console.WriteLine("Done.");     } }