73 lines
1.8 KiB
C#
73 lines
1.8 KiB
C#
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using System.Threading;
|
|
using MySql.Data.MySqlClient;
|
|
using UnityEngine;
|
|
|
|
|
|
public class GetSQL : MonoBehaviour
|
|
{
|
|
public DataScriptableObject dataScriptableObject;
|
|
|
|
private string LinkInfo;
|
|
|
|
private string SQLstatement;
|
|
|
|
private string datalist;
|
|
|
|
private List<string> data;
|
|
|
|
public bool isLink;
|
|
// Start is called before the first frame update
|
|
void Start()
|
|
{
|
|
LinkInfo = "server="+dataScriptableObject.IP+";" +
|
|
"port="+dataScriptableObject.Port+";" +
|
|
"database="+dataScriptableObject.Database+";" +
|
|
"user="+dataScriptableObject.User+";" +
|
|
"password="+dataScriptableObject.Password+";" +
|
|
"charset="+dataScriptableObject.Charset;
|
|
}
|
|
|
|
|
|
public List<string> getdata()
|
|
{
|
|
return data;
|
|
}
|
|
public void GetSqlData()
|
|
{
|
|
Thread thread = new Thread(new ThreadStart(ThreadGetSqlData));//创建网络请求的线程
|
|
thread.Start();//启动线程
|
|
}
|
|
|
|
public void ThreadGetSqlData()
|
|
{
|
|
while (isLink)
|
|
{
|
|
List<string> data = new List<string>();
|
|
data.Clear();
|
|
MySqlConnection con = new MySqlConnection(LinkInfo);
|
|
try
|
|
{
|
|
con.Open();
|
|
Debug.Log("连接成功");
|
|
}
|
|
catch (MySqlException e)
|
|
{
|
|
e.ToString();//解决Warming
|
|
Debug.Log("连接失败");
|
|
}
|
|
|
|
MySqlCommand cmd = new MySqlCommand(SQLstatement, con);
|
|
MySqlDataReader reader = cmd.ExecuteReader();
|
|
while (reader.Read())
|
|
{
|
|
data.Add(reader.GetString(datalist).ToString());
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
}
|