需要准备的Dll:
MySql.Data.dll
using MySql.Data.MySqlClient;
using UnityEngine;
public class MySqlTest : MonoBehaviour
{
private string DataSource = "127.0.0.1";
private string Database = "mysql";
private string UserID = "root";
private string Password = "123456";
private void Start()
{
#region 构造一个 数据库连接者"MySqlConnection"
string connectData =
"Database = " + Database + "; " +
"Data Source=" + DataSource + ";" +
"User ID=" + UserID + ";" +
"Password=" + Password;
MySqlConnection connection = new MySqlConnection(connectData);
#endregion
#region 连接数据库
bool isConcent = false;
try
{
connection.Open();
isConcent = true;
}
catch (System.Exception e)
{
throw new System.Exception(e.ToString());
}
#endregion
#region 数据库执行指令,获取返回值
if (isConcent)
{
string command = "SELECT Question FROM _mathf";
MySqlCommand mySqlCommand = new MySqlCommand(command, connection);
MySqlDataReader reader = mySqlCommand.ExecuteReader();
while (reader.Read() == true)
{
print(reader.GetString(0));
}
connection.Close();
}
#endregion
}
}