Unity connects to Mysql database (detailed tutorial)

Unity connects to Mysql in detail

1. You need to reference a MySql.Data.dll file in unity (if you can’t find this file in the mysql you installed, you can download one directly from the Internet) and put this file under Assets-Plugins, refer to using MySql.Data.MySqlClient in the code; string constr = “Database=zzz;Data Source=127.0.0.1;user=root;Password=12345 for local connection to Mysql 6;pooling=false;charset=utf8;port=
insert image description here
3306
"
;

Example: connect to Mysql and perform fuzzy search

using MySql.Data.MySqlClient;

public class Test : MonoBehaviour
{
    
    
	public InputField findInput;
	string constr = "Database=zzz;Data Source=127.0.0.1;user=root;Password=123456;pooling=false;charset=utf8;port=3306";
	void Start()
	{
    
    
		DataFind();
	}
	private void DataFind()
	{
    
    
		//初始化数据库
		MysqlConnection con=new MysqlConnection(constr);
		con.Open();
	
		//模糊查找
		string findData = "select * from store where 名称 like '%"+findInput.text+"%' ";
       	MySqlCommand myCom = new MySqlCommand(findData, myCon);
        MySqlDataReader reader = myCom.ExecuteReader();
        string name = reader.GetString("名称");
        string number = reader.GetString("数量");
        //循环读取查到符合条件的数据
        while (reader.Read())
        {
    
    
            Debug.Log(name+number);
        }
        con.Close();
	}
}

Guess you like

Origin blog.csdn.net/qq_45598937/article/details/131086172