unity访问mysql数据库

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

public class mysql : MonoBehaviour
    
{
    public Text wenzi;
     string id;
    // Start is called before the first frame update
    private void Start()
    {
        ;
        string constructorString = "datasource=127.0.0.1;port=3306;database=student;user=root;pwd=xiaobei;";
        MySqlConnectiona conn = new MySqlConnection(constructorString);
        try
        {
            conn.Open();
            Debug.Log("已经建立连接");
            string sql = "select * from weizhi where id=2";
            MySqlCommand cmd = new MySqlCommand(sql, conn);
            MySqlDataReader rdr = cmd.ExecuteReader();
            
            if(rdr.Read())
            {
                 wenzi.text = rdr.GetString("author")+ rdr.GetString("content");
               
            }
            Debug.Log(id);

        }
        catch (MySqlException ex)
        {
            Debug.Log(ex.Message);
           
        }
        finally
        {
            conn.Close();
            Debug.Log("guanbi");
        }
    }
   


    // Update is called once per frame
   
}

 

你可能感兴趣的:(unity)