unity读取mysql5.7版本示例

使用了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();
    }
}
相关推荐
suzh1qian15 小时前
Unity类银河战士恶魔城学习总结(P156 Audio Settings音频设置)
学习·unity·c#·游戏引擎
fcm1917 小时前
untiy之碰撞体编辑器
游戏·unity
大梦百万秋17 小时前
C++ 游戏开发:跨平台游戏引擎的构建与优化
开发语言·c++·游戏引擎
tealcwu19 小时前
【Unity基础】Unity中Transform.forward的详解与应用
unity·游戏引擎
W Y1 天前
【Unity-摩擦力】
unity·游戏引擎
suzh1qian1 天前
Unity类银河战士恶魔城学习总结(P155 More example on audio effects更多的音效细节)
学习·游戏·unity·c#·游戏引擎
Lowjin_1 天前
Unity中,Canva的三种渲染模式
unity·游戏引擎
小白球滚芝麻1 天前
Unity下载文件断点续下
android·unity·c#·游戏引擎·vr
半夏知半秋2 天前
unity中控制相机跟随物体移动
笔记·数码相机·unity·c#·游戏引擎