Unity连接Mysql

先放关键类

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

public class ConnectMysql {
    private static ConnectMysql instance;
    private string mDBName = "student";
    private string mHost = "localhost";
    private string mUser = "jxd";
    private string mPassword = "1234";
    private  MySqlConnection mCon=null;
    /// 
    /// 使用单利模式
    /// 
    public static ConnectMysql Instance
    {
        get
        {
            if (instance==null)
            {
                instance = new ConnectMysql();
               
            }
            return instance;
        }
        
    }
    public ConnectMysql()
    {
      string mSql = string.Format("Database={0};Data Source={1};User Id={2};Password={3};charset={5};port={4};", mDBName, mHost, mUser, mPassword, "3306","utf8");
      mCon = new MySqlConnection(mSql);
    }
    /// 
    /// 打开数据库
    /// 
    public void OpenMysql()
    {
        try
        {
            mCon.Open();
            Debug.Log("数据库已连接");
        }
        catch (Exception e)
        {
            Debug.Log("数据库连接失败"+e);
        }
    }
    /// 
    /// 创建表
    /// 
    /// 
    /// 
    public void CreateTable(string sql)
    {
        MySqlCommand com = new MySqlCommand(sql, mCon);
        int res = com.ExecuteNonQuery();
    }
    /// 
    /// 插入数据
    /// 
    /// 
    /// 
    public void Insert(string sql)
    {
        MySqlCommand com = new MySqlCommand(sql, mCon);
        int res = com.ExecuteNonQuery();
    }
    /// 
    /// 删除数据
    /// 
    /// 
    /// 
    public void Del(string sql)
    {
        MySqlCommand com = new MySqlCommand(sql, mCon);
        int res = com.ExecuteNonQuery();
        Debug.Log(res);
    }
    /// 
    /// 添加数据
    /// 
    /// 
    /// 
    public void Add(string sql)
    {
        MySqlCommand com = new MySqlCommand(sql, mCon);
        int res = com.ExecuteNonQuery();
    }
    /// 
    /// 更新数据
    /// 
    /// 
    /// 
    public void Update(string sql)
    {
        MySqlCommand com = new MySqlCommand(sql, mCon);
        int res = com.ExecuteNonQuery();
    }
    /// 
    /// 读数据
    /// 
    /// 
    /// 
    /// 
    public string Query(string sql)
    {
        if (mCon.State==ConnectionState.Open)
        {
            MySqlCommand com = new MySqlCommand(sql,mCon);
            MySqlDataReader reader = com.ExecuteReader();
            try
            {
                if (reader.HasRows)
                {
                    while (true)
                    {
                        if (reader.Read())
                        {
                            for (int i = 0; i < reader.FieldCount; i++)
                            {
                                Debug.Log(reader[i].ToString());
                                
                            }
                        }
                    }
                }
                else
                {
                    Debug.Log("空表");
                }
            }
            catch (Exception e)
            {
                Debug.Log(e);
               
            }
            finally
            {
                reader.Close();
            }
            return reader[0].ToString();
        }
        else
        {
            return null;
        }
    }
    /// 
    /// 关闭数据库
    /// 
    public void Close()
    {
        mCon.Close();
        mCon.Dispose();
        Debug.Log("关闭数据库");
    }
}

Mysql中表的格式
Unity连接Mysql_第1张图片
测试类(挂在场景中任意物体上)

using System.Data;
using UnityEngine;
using UnityEngine.UI;
public class Test : MonoBehaviour {
    
	// Use this for initialization
	void Start () {
    }
	
	// Update is called once per frame
	void Update () {
        if (Input.GetKeyDown(KeyCode.A))
        {
            ConnectMysql.Instance.OpenMysql();
            transform.GetComponent().text= ConnectMysql.Instance.Query("select * from students");
            ConnectMysql.Instance.Close();
        }
        if (Input.GetKeyDown(KeyCode.S))
        {
            ConnectMysql.Instance.OpenMysql();
            ConnectMysql.Instance.Add("insert into students(id,name,sex,age)values(3,'是的',0,22)");
            ConnectMysql.Instance.Close();
        }
        if (Input.GetKeyDown(KeyCode.D))
        {
            ConnectMysql.Instance.OpenMysql();
            ConnectMysql.Instance.Update("update students set name='张三' where id=2");
            ConnectMysql.Instance.Close();
        }
     
    }
}

你可能感兴趣的:(C#,Unity,Mysql)