使用了miniexcel插件,与mydata.dll 。
csharp
using MiniExcelLibs;
using MySql.Data.MySqlClient;
using System.Collections.Generic;
using System.Data;
using System.Text;
using UnityEngine;
public class LoadMySQL_虚拟仿真 : DataLayerBase<Dictionary<string, string>>
{
public static LoadMySQL_虚拟仿真 Ins;
string server = "192.169.4.102";
string database = "avatar_model";
string user = "model";
string password = "avatar@model";
MySqlConnection connection;
string SQL = @"SELECT {0} FROM {1}";
string SetMysql =@"
UPDATE {1}
SET {0} = '{2}'
WHERE {0} = '{0}';
";
public class XNFZ_Data
{
public string TagName { get; set; }
public string DataType { get; set; }
public string ReadWrite { get; set; }
public string ScanRate_ms { get; set; }
public string Description { get; set; }
public string Value { get; set; }
}
List<string> sqlList = new List<string>();
public static Dictionary<string, XNFZ_Data> XNFZ_keyValues = new Dictionary<string, XNFZ_Data>();
protected override void Awake()
{
base.Awake();
Ins = this;
LoadExcel(Application.streamingAssetsPath + "/宁波数据展示点位V3.xlsx", "IDEAS");
joinFind();
foreach (var item in XNFZ_keyValues)
{
sqlList.Add(item.Key);
}
}
protected override void RequestData(ref Dictionary<string, string> Data)
{
if (XNFZ_keyValues.Count > 0)
{
StringBuilderSQL(sqlList, "model_data_display_point");
}
}
public void SetDataValue_修改数据库字段值(string tagname,string value)
{
// SetMysql = string.Format(SetMysql,);
}
void joinFind()
{
string sqlSer = "server = " + server + ";" + "Database =" + database + ";" +
"user =" + user + ";" +
"password =" + password;
connection = new MySqlConnection(sqlSer);
try
{
connection.Open();
Debug.Log("数据库连接成功!!!!!");
}
catch (System.Exception e)
{
Debug.LogError("数据库连接失败" + e.Message);
return;
}
if (connection.State != ConnectionState.Open)
return;
}
void StringBuilderSQL(List<string> sqlList, string sqlcharname)
{
StringBuilder sql = new StringBuilder();
StringBuilder queryTagName = new StringBuilder();
for (int i = 0; i < sqlList.Count; i++)
{
queryTagName.Append(sqlList[i]);
queryTagName.Append(",");
}
queryTagName.Remove(queryTagName.Length - 1, 1);
sql.Append(string.Format(SQL, queryTagName, sqlcharname));
DataSet ds = Query(sql.ToString());
if (ds != null && ds.Tables.Count > 0)
{
for (int i = 0; i < sqlList.Count; i++)
{
string sqlTagName = ds.Tables[0].Rows[0][sqlList[i]].ToString().Replace(" ", "");
foreach (var item in XNFZ_keyValues)
{
if (string.Equals(sqlList[i], item.Key))
{
if (string.IsNullOrEmpty(sqlTagName))
{
Debug.Log(sqlList[i]);
}
else
{
item.Value.Value = sqlTagName;
}
}
}
}
}
}
public DataSet Query(string sql)
{
DataSet ds = new DataSet();
MySqlCommand command = new MySqlCommand(sql, connection);
command.CommandTimeout = 100;
MySqlDataAdapter adapter = new MySqlDataAdapter(command);
adapter.Fill(ds);
return ds;
}
void LoadExcel(string path, string sheel)
{
var rows = MiniExcel.Query<XNFZ_Data>(path, sheel, ExcelType.XLSX, "A1");
//using (var stream = File.OpenRead(path))
//{
// var rows = stream.Query<XNFZ_Data>();
if (rows != null)
{
foreach (XNFZ_Data item in rows)
{
if (!string.IsNullOrEmpty(item.TagName))
{
XNFZ_keyValues.Add(item.TagName, item);
}
else
{
Debug.LogError(sheel + $"中重复或空的DataName:{item.TagName}");
}
}
}
else
{
Debug.LogError("excel加载失败");
}
}
public static XNFZ_Data GetDataName(string dataname)
{
if (XNFZ_keyValues.ContainsKey(dataname))
{
return XNFZ_keyValues[dataname];
}
else { Debug.Log("字典中不包含" + dataname); return null; }
}
protected override void OnDisable()
{
base.OnDisable();
connection.Close();
}
}