I have just started using C# and do not know much about it. I am using the 3d game engine called Unity and trying to write a c# script to access the MySQL database that I am
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.
You can find a procedure in Unity Wiki with PHP, MySQL and C#/JavaScript.
It consists of three steps
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.");
}
}
A late answer, but maybe this will help others.
I can actually access my server side sql database on my non-game Unity app. In my case, my app controls whether the user serial number true or not. After users entered their serial key my app starts to compare the serial numbers on sql database. You can use this Server Side Highscores Tutorial for your start point. I did the same. Follow these steps then you should be able to acces your sql database on Unity.
Database (SQLite) Setup for Unity Create new folder
Create new folder under Assets Folder Rename it Plugins.
Copy sqlite3.def and sqlite3.dll into Assets/Plugins in your unity project .You can download these files here http://www.sqlite.org/download.html for windows (Precompiled Binaries for Windows)
Replace PickAndPlaceDatabase.s3db with your database name
under Assets Folder Rename it Plugins .
void Start () {
string conn = "URI=file:" + Application.dataPath + "/PickAndPlaceDatabase.s3db"; //Path to database.
IDbConnection dbconn;
dbconn = (IDbConnection) new SqliteConnection(conn);
dbconn.Open(); //Open connection to the database.
IDbCommand dbcmd = dbconn.CreateCommand();
string sqlQuery = "SELECT value,name, randomSequence " + "FROM PlaceSequence";
dbcmd.CommandText = sqlQuery;
IDataReader reader = dbcmd.ExecuteReader();
while (reader.Read())
{
int value = reader.GetInt32(0);
string name = reader.GetString(1);
int rand = reader.GetInt32(2);
Debug.Log( "value= "+value+" name ="+name+" random ="+ rand);
}
reader.Close();
reader = null;
dbcmd.Dispose();
dbcmd = null;
dbconn.Close();
dbconn = null;
}
these is useful link:
Further SQLite Help Visit : http://www.tutorialspoint.com/sqlite/
http://wiki.unity3d.com/index.php/Webservices_In_Unity then you should add this library into your C# code :
using AssemblyCSharp.portal.wwwww.com;
now you can use it:
using UnityEngine;
using System.Collections;
using AssemblyCSharp.portal.wwwww.com;
public class Game5_Player : MonoBehaviour
{
public string Logo;
void Start ()
{
crm1 s = new crm1();
Logo= s.getCompanyLogo ();
}
}
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.