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;
}
}
}