一、引用如下所示对应的库文件
(I18N.CJK.dll、I18N.dll、I18N.West.dll、System.Data.dll、System.Drawing.dll、MySql.Data.dll共计6个,其中MySql.Data.dll需要单独下载,其余的5个库文件可以在Unity的安装目录()下找到):下载地址:https://pan.baidu.com/s/1yQ0_R5I483SnyywIuwBRAA
提取码:4iil
二、编写对应的操作MySQL数据库的控制脚本
1、编写通用的MySQL数据库操作类
/***
* Title:"智慧工厂" 项目
* 主题:通用的MySQL数据库操作类
* Description:
* 功能:实现数据库的基础增、删、查、改操作
* Date:2019
* Version:0.1版本
* Author:Coffee
* Modify Recoder:
*/
using System;
using System.Data;
using System.Collections.Generic;
using MySql.Data.MySqlClient;
namespace kernal
{
public class MySQLHelper
{
private string connectionString;
///
/// 数据库连接定义
///
//private SqlConnection dbConnection;
private MySqlConnection dbConnection;
///
/// SQL命令定义
///
private MySqlCommand dbCommand;
///
/// 数据读取定义
///
private MySqlDataAdapter dataReader;
///
/// 设置数据库连接字符串
///
public string ConnectionString
{
set { connectionString = value; }
}
///
/// 构造函数
///
/// 数据库连接字符串
public MySQLHelper(string connectionString)
{
this.connectionString = connectionString;
}
///
/// 执行一个查询,并返回结果集
///
/// 要执行的查询SQL文本命令
/// 返回查询结果集
public DataTable ExecuteDataTable(string sql)
{
return ExecuteDataTable(sql, CommandType.Text, null);
}
///
/// 执行一个查询,并返回查询结果
///
/// 要执行的SQL语句
/// 要执行的查询语句的类型,如存储过程或者SQL文本命令
/// 返回查询结果集
public DataTable ExecuteDataTable(string sql, CommandType commandType)
{
return ExecuteDataTable(sql, commandType, null);
}
///
/// 执行一个查询,并返回查询结果
///
/// 要执行的SQL语句
/// 要执行的查询语句的类型,如存储过程或者SQL文本命令
/// Transact-SQL 语句或存储过程的参数数组
///
public DataTable ExecuteDataTable(string sql, CommandType commandType, MySqlParameter[] parameters)
{
DataTable data = new DataTable();//实例化DataTable,用于装载查询结果集
using (MySqlConnection connection = new MySqlConnection(connectionString))
{
using (MySqlCommand command = new MySqlCommand(sql, connection))
{
command.CommandType = commandType;//设置command的CommandType为指定的CommandType
//如果同时传入了参数,则添加这些参数
if (parameters != null)
{
foreach (MySqlParameter parameter in parameters)
{
command.Parameters.Add(parameter);
}
}
//通过包含查询SQL的MySqlCommand实例来实例化SqlDataAdapter
MySqlDataAdapter adapter = new MySqlDataAdapter(command);
adapter.Fill(data);//填充DataTable
}
}
return data;
}
///
///
///
/// 要执行的查询SQL文本命令
///
public MySqlDataReader ExecuteReader(string sql)
{
return ExecuteReader(sql, CommandType.Text, null);
}
///
///
///
/// 要执行的SQL语句
/// 要执行的查询语句的类型,如存储过程或者SQL文本命令
///
public MySqlDataReader ExecuteReader(string sql, CommandType commandType)
{
return ExecuteReader(sql, commandType, null);
}
///
///
///
/// 要执行的SQL语句
/// 要执行的查询语句的类型,如存储过程或者SQL文本命令
/// Transact-SQL 语句或存储过程的参数数组
///
public MySqlDataReader ExecuteReader(string sql, CommandType commandType, MySqlParameter[] parameters)
{
MySqlConnection connection = new MySqlConnection(connectionString);
MySqlCommand command = new MySqlCommand(sql, connection);
//如果同时传入了参数,则添加这些参数
if (parameters != null)
{
foreach (MySqlParameter parameter in parameters)
{
command.Parameters.Add(parameter);
}
}
connection.Open();
//CommandBehavior.CloseConnection参数指示关闭Reader对象时关闭与其关联的Connection对象
return command.ExecuteReader(CommandBehavior.CloseConnection);
}
///
///
///
/// 要执行的查询SQL文本命令
///
public Object ExecuteScalar(string sql)
{
return ExecuteScalar(sql, CommandType.Text, null);
}
///
///
///
/// 要执行的SQL语句
/// 要执行的查询语句的类型,如存储过程或者SQL文本命令
///
public Object ExecuteScalar(string sql, CommandType commandType)
{
return ExecuteScalar(sql, commandType, null);
}
///
///
///
/// 要执行的SQL语句
/// 要执行的查询语句的类型,如存储过程或者SQL文本命令
/// Transact-SQL 语句或存储过程的参数数组
///
public Object ExecuteScalar(string sql, CommandType commandType, MySqlParameter[] parameters)
{
object result = null;
using (MySqlConnection connection = new MySqlConnection(connectionString))
{
using (MySqlCommand command = new MySqlCommand(sql, connection))
{
command.CommandType = commandType;//设置command的CommandType为指定的CommandType
//如果同时传入了参数,则添加这些参数
if (parameters != null)
{
foreach (MySqlParameter parameter in parameters)
{
command.Parameters.Add(parameter);
}
}
connection.Open();//打开数据库连接
result = command.ExecuteScalar();
}
}
return result;//返回查询结果的第一行第一列,忽略其它行和列
}
///
/// 对数据库执行增删改操作
///
/// 要执行的查询SQL文本命令
///
public int ExecuteNonQuery(string sql)
{
return ExecuteNonQuery(sql, CommandType.Text, null);
}
///
/// 对数据库执行增删改操作
///
/// 要执行的SQL语句
/// 要执行的查询语句的类型,如存储过程或者SQL文本命令
///
public int ExecuteNonQuery(string sql, CommandType commandType)
{
return ExecuteNonQuery(sql, commandType, null);
}
///
/// 对数据库执行增删改操作
///
/// 要执行的SQL语句
/// 要执行的查询语句的类型,如存储过程或者SQL文本命令
/// Transact-SQL 语句或存储过程的参数数组
///
public int ExecuteNonQuery(string sql, CommandType commandType, MySqlParameter[] parameters)
{
int count = 0;
using (MySqlConnection connection = new MySqlConnection(connectionString))
{
using (MySqlCommand command = new MySqlCommand(sql, connection))
{
command.CommandType = commandType;//设置command的CommandType为指定的CommandType
//如果同时传入了参数,则添加这些参数
if (parameters != null)
{
foreach (MySqlParameter parameter in parameters)
{
command.Parameters.Add(parameter);
}
}
connection.Open();//打开数据库连接
count = command.ExecuteNonQuery();
}
}
return count;//返回执行增删改操作之后,数据库中受影响的行数
}
///
/// 返回当前连接的数据库中所有由用户创建的数据库
///
///
public DataTable GetTables()
{
DataTable data = null;
using (MySqlConnection connection = new MySqlConnection(connectionString))
{
connection.Open();//打开数据库连接
data = connection.GetSchema("Tables");
}
return data;
}
///
/// 执行多条SQL语句,实现数据库事务。
///
/// 多条SQL语句
public int ExecuteSqlTran(List SQLStringList)
{
using (MySqlConnection connection = new MySqlConnection(connectionString))
{
connection.Open();
MySqlCommand cmd = new MySqlCommand();
cmd.Connection = connection;
MySqlTransaction tx = connection.BeginTransaction();
cmd.Transaction = tx;
try
{
int count = 0;
for (int n = 0; n < SQLStringList.Count; n++)
{
string strsql = SQLStringList[n];
if (strsql.Trim().Length > 1)
{
cmd.CommandText = strsql;
count += cmd.ExecuteNonQuery();
}
}
tx.Commit();
return count;
}
catch
{
tx.Rollback();
return 0;
}
}
}
///
/// 执行带一个存储过程参数的的SQL语句。
///
/// SQL语句
/// 参数内容,比如一个字段是格式复杂的文章,有特殊符号,可以通过这个方式添加
/// 影响的记录数
public int ExecuteSql(string SQLString, string content)
{
using (MySqlConnection connection = new MySqlConnection(connectionString))
{
MySqlCommand cmd = new MySqlCommand(SQLString, connection);
MySqlParameter myParameter = new MySqlParameter("@content", SqlDbType.NText);
myParameter.Value = content;
cmd.Parameters.Add(myParameter);
try
{
connection.Open();
int rows = cmd.ExecuteNonQuery();
return rows;
}
catch (Exception e)
{
throw e;
}
finally
{
cmd.Dispose();
connection.Close();
}
}
}
///
/// 执行带一个存储过程参数的的SQL语句。
///
/// SQL语句
/// 参数内容,比如一个字段是格式复杂的文章,有特殊符号,可以通过这个方式添加
/// 影响的记录数
public object ExecuteSqlGet(string SQLString, string content)
{
using (MySqlConnection connection = new MySqlConnection(connectionString))
{
MySqlCommand cmd = new MySqlCommand(SQLString, connection);
MySqlParameter myParameter = new MySqlParameter("@content", SqlDbType.NText);
myParameter.Value = content;
cmd.Parameters.Add(myParameter);
try
{
connection.Open();
object obj = cmd.ExecuteScalar();
if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value)))
{
return null;
}
else
{
return obj;
}
}
catch (Exception e)
{
throw e;
}
finally
{
cmd.Dispose();
connection.Close();
}
}
}
///
/// 向数据库里插入图像格式的字段(和上面情况类似的另一种实例)
///
/// SQL语句
/// 图像字节,数据库的字段类型为image的情况
/// 影响的记录数
public int ExecuteSqlInsertImg(string strSQL, byte[] fs)
{
using (MySqlConnection connection = new MySqlConnection(connectionString))
{
MySqlCommand cmd = new MySqlCommand(strSQL, connection);
MySqlParameter myParameter = new MySqlParameter("@fs", SqlDbType.Image);
myParameter.Value = fs;
cmd.Parameters.Add(myParameter);
try
{
connection.Open();
int rows = cmd.ExecuteNonQuery();
return rows;
}
catch (Exception e)
{
throw e;
}
finally
{
cmd.Dispose();
connection.Close();
}
}
}
///
/// 执行查询语句,返回DataSet
///
/// 查询语句
/// DataSet
public DataSet Query(string SQLString)
{
using (MySqlConnection connection = new MySqlConnection(connectionString))
{
DataSet ds = new DataSet();
try
{
connection.Open();
MySqlDataAdapter command = new MySqlDataAdapter(SQLString, connection);
command.Fill(ds, "ds");
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
return ds;
}
}
}//Class_end
}
2、 编写连接到MySQL数据库的脚本提取对应表的内容(这里以提取权限表信息为例说明)
/***
* Title:"智慧工厂 " 项目
* 主题:读取权限表信息
* Description:
* 功能:XXX
* Date:2019
* Version:0.1版本
* Author:Coffee
* Modify Recoder:
*/
using kernal;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using UnityEngine;
using UnityEngine.UI;
namespace Test
{
public class Test_ReadTM_Authority : MonoBehaviour
{
public Text authorityText; //显示文本
private string conString = string.Format("Server=127.0.0.1;Database=testmysql;User ID=sanertest;Password=123456;Port=3306;");
void Start()
{
//显示获取到的数据信息
DisplayTM_AuthorityInfos(authorityText);
}
///
/// 显示获取到的数据信息
///
/// 显示内容的文本
private void DisplayTM_AuthorityInfos(Text text)
{
if (text!=null)
{
//清空文本内容
text.text = null;
List _AuthorityDatas;
_AuthorityDatas = GetAllTM_AuthorityInfos();
int count = _AuthorityDatas.Count;
if (count > 0)
{
for (int i = 0; i < count; i++)
{
text.text += " " + _AuthorityDatas[i].ID + " " + _AuthorityDatas[i].Name + " " + _AuthorityDatas[i].Dec;
}
}
else
{
text.text = "没有获取到数据信息!!!";
}
}
}
///
/// 获取到权限信息表的所有信息
///
///
private List GetAllTM_AuthorityInfos()
{
List _AuthorityDatas = new List();
try
{
string sql = "SELECT * FROM tm_authority";
MySQLHelper db = new MySQLHelper(conString);
DataTable data = db.ExecuteDataTable(sql);
int count = data.Rows.Count;
if (count > 0)
{
for (int i = 0; i < count; i++)
{
DataRow row = data.Rows[i];
TM_AuthorityData _AuthorityData = new TM_AuthorityData()
{
ID = int.Parse(row["ID"].ToString()),
Name = (row["Name"].ToString()),
Dec = (row["Dec"].ToString()),
};
_AuthorityDatas.Add(_AuthorityData);
}
}
}
catch (Exception e)
{
throw e;
}
return _AuthorityDatas;
}
}//Class_end
}
权限表的属性脚本如下:
/***
* Title:"智慧工厂" 项目
* 主题:权限信息表的属性
* Description:
* 功能:XXX
* Date:2019
* Version:0.1版本
* Author:Coffee
* Modify Recoder:
*/
namespace Test
{
public class TM_AuthorityData
{
private int _ID; //编号
private string _Name; //权限名称
private string _Dec; //权限描述
public int ID
{
get
{
return _ID;
}
set
{
_ID = value;
}
}
public string Name
{
get
{
return _Name;
}
set
{
_Name = value;
}
}
public string Dec
{
get
{
return _Dec;
}
set
{
_Dec = value;
}
}
}//Class_end
}
三、将脚本添加到面板上,指定对应的显示文本,运行即可
项目源码下载地址: https://download.csdn.net/download/xiaochenxihua/10959530
注意:本次项目使用的MySQL数据库下载地址:https://download.csdn.net/download/xiaochenxihua/10959515
使用navicat for mysql 数据库备份与还原 参考链接:https://www.cnblogs.com/helkbore/p/5741694.html
MySQL对应的各个版本的库文件下载地址:链接:https://pan.baidu.com/s/1STi-qDoudFG62roNJ_Al6A
提取码:tw3f
项目参考链接:https://my.oschina.net/smartsmile/blog/814997
https://www.cnblogs.com/Caveolae/p/7058890.html
https://stackoverflow.com/questions/41930040/net-mysql-error-the-given-key-was-not-present-in-the-dictionary
https://blog.csdn.net/mmaey/article/details/8128500
http://www.runoob.com/mysql/mysql-create-tables.html