unity 连接MySQL读取数据

using MySql.Data.MySqlClient;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using UnityEngine;

public class MySQLManager : Singleton<MySQLManager> {

    string conStr = "server=127.0.0.1;port=3306;User Id = root;password=123456;Database=test_db;charset=utf8";
    //string selStr = "SELECT * FROM v_hdxx";
    MySqlConnection myCon;


    public List<DataRow> GetDatas(DataSet ds)
    {
        List<DataRow> conList = new List<DataRow>();
        //Debug.Log(string.Format("行:{0},列:{1}", ds.Tables[0].Rows.Count, ds.Tables[0].Columns.Count));
        for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
        {
            //Debug.Log(ds.Tables[0].Rows[i]["xh"]);
            DataRow row = ds.Tables[0].Rows[i];
            conList.Add(row);
        }
        return conList;
    }

  
    public DataSet GetSqlCon(string selStr)//查询数据
    {

        myCon = new MySqlConnection(conStr); //数据库登录数据

        myCon.Open();//打开连接
        DataSet ds = new DataSet();
        try
        {
            MySqlDataAdapter da = new MySqlDataAdapter(selStr, myCon);
            da.Fill(ds);
        }
        catch (Exception e)
        {
            throw new Exception("SQL:" + selStr + "\n" + e.Message.ToString());
        }

        myCon.Close();
        return ds;
    }
}

    public string selStr = "SELECT * FROM v_kqxx";

    public List<DataRow> list_Data;

    void GetDataAndCreate()
    {
        DataSet dataSet = MySQLManager.Instance.GetSqlCon(selStr);
        list_Data = MySQLManager.Instance.GetDatas(dataSet);
    }


    void CreateItem()
    {
        foreach (var item in list_Data)
        {
            GameObject go = new GameObject();

            CabinetDataItem cabinetDataItem = go.AddComponent<CabinetDataItem>();
            cabinetDataItem.cwghh = item["cwghh"].ToString();
            cabinetDataItem.cwglh = item["cwglh"].ToString();
            cabinetDataItem.cwghm = item["cwghm"].ToString();
            cabinetDataItem.kssj = item["kssj"].ToString();
            cabinetDataItem.jssj = item["jssj"].ToString();
        }
    }

    public class CabinetDataItem : MonoBehaviour {

        public Button button;
        public string cwghh;
        public string cwglh;
        public string cwghm;
        public string pcmc;
        public string kssj;
        public string jssj;
        public string syzt;

        public CabinetDataItem(string cwghh, string cwglh, string cwghm, string pcmc, string kssj, string jssj, string syzt)
        {
            this.cwghh = cwghh;
            this.cwglh = cwglh;
            this.cwghm = cwghm;
            this.pcmc = pcmc;
            this.kssj = kssj;
            this.jssj = jssj;
            this.syzt = syzt;
        }
    }
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值