using UnityEngine;

using UnityEngine.UI;

using System;

using System.Collections;

using System.Data;

using MySql.Data.MySqlClient;

public class Test : MonoBehaviour

{

    public Text t;

    void Start()

    {

        string constr = "server=localhost;Database=test;User Id=root;password=root";

        //建立连接的语句

        //如果是本地数据库server为localhost,不是则输入server的地址

        MySqlConnection mycon = new MySqlConnection(constr); //建立连接

        mycon.Open();

        //查询指令

        string selstr = "select zhuansu1 from zhuansubiao where xuelie=1";

        MySqlCommand myselect = new MySqlCommand(selstr, mycon);

        DataSet ds = new DataSet();

        try

        {

            MySqlDataAdapter da = new MySqlDataAdapter(selstr, mycon);

            da.Fill(ds);

          t.text=ds.Tables[0].Rows[0][0].ToString();

            Debug.Log("Query success!"+ t.text);

            print(ds.Tables[0].Rows[0][0]);

        }

        catch (Exception ee)

        {

            throw new Exception("SQL: " + selstr + "\n" + ee.Message.ToString());

        }

        mycon.Close(); //关闭连接

    }

}

更多推荐