I need some help. I’m writing a C# script (I’m new to the world of C#) and I’m getting the following error after I begin running my Scene:
Line 28 is:
SQLdb.Start();
What I am trying to do is access a mySQL database. Store the results in some type of object that I can return to a second script, which will populate the resulting data into a TreeView control.
Here’s my code: AddClass.cs
void Start ()
{
// Setup the TreeView to retrieve Database data.
m_myTreeView = gameObject.GetComponent<TreeViewControl>();
if (null == m_myTreeView)
{
Debug.LogError("Use the tree view menu to add the control");
return;
}
// TreeViewItem item1 = item.RootItem.AddItem("Playable Races and Classes");
// Setup the Database functions.
SQLdb = gameObject.GetComponent<SQLDatabase>();
// Open a database connection and retrieve the data for Classes.
SQLdb.Start();
PopulateTreeData();
}
SQLDatabase.cs
using UnityEngine;
using System;
using System.Collections;
using System.Data;
using MySql.Data.MySqlClient;
public class SQLDatabase : MonoBehaviour {
// Global variables
private static IDbConnection dbConnection;
// Initialisation
public void Start() {
openSqlConnection();
}
// On quit
public void OnApplicationQuit() {
closeSqlConnection();
}
// Connect to database
private static void openSqlConnection() {
... Omitted for security ...
}
// Disconnect from database
private static void closeSqlConnection() {
dbConnection.Close();
dbConnection = null;
Debug.Log("Disconnected from database.");
}
// MySQL Query
public static ArrayList doQuery(string sqlQuery) {
IDbCommand dbCommand = dbConnection.CreateCommand();
dbCommand.CommandText = sqlQuery;
// IDataReader reader = dbCommand.ExecuteReader();
ArrayList rowList = new ArrayList();
IDataReader reader = dbCommand.ExecuteReader();
while (reader.Read())
{
object[] values = new object[reader.FieldCount];
reader.GetValues(values);
rowList.Add(values);
}
reader.Close();
reader = null;
dbCommand.Dispose();
dbCommand = null;
return rowList;
}
I would really appreciate any help you all could provide. Thank you!