usingUnityEngine;
usingSystem;
usingSystem.Collections;
usingSystem.Data;
usingMySql.Data.MySqlClient;
publicclassCMySql : MonoBehaviour {
// Global variables
privatestaticMySqlConnection dbConnection;
staticstringhost ="192.168.1.100";
staticstringid ="蛮牛教育";
staticstringpwd ="123456";
staticstringresult ="";
voidOnGUI()
{
host = GUILayout.TextField( host, 200, GUILayout.Width(200));
id = GUILayout.TextField( id, 200, GUILayout.Width(200));
pwd = GUILayout.TextField( pwd, 200, GUILayout.Width(200));
if(GUILayout.Button("Test"))
{
stringconnectionString ="Server="+host+";Database=test;User ID="+id+";Password="+pwd+";Pooling=false";
openSqlConnection( connectionString );
}
GUILayout.Label(result);
}
// On quit
publicstaticvoidOnApplicationQuit() {
closeSqlConnection();
}
// Connect to database
privatestaticvoidopenSqlConnection(stringconnectionString) {
dbConnection =newMySqlConnection(connectionString);
dbConnection.Open();
result = dbConnection.ServerVersion;
Debug.Log("Connected to database."+result);
}
// Disconnect from database
privatestaticvoidcloseSqlConnection() {
dbConnection.Close();
dbConnection =null;
//Debug.Log("Disconnected from database."+result);
}
// MySQL Query
publicstaticvoiddoQuery(stringsqlQuery) {
IDbCommand dbCommand = dbConnection.CreateCommand();
dbCommand.CommandText = sqlQuery;
IDataReader reader = dbCommand.ExecuteReader();
reader.Close();
reader =null;
dbCommand.Dispose();
dbCommand =null;
}