unity3d串連mysql

來源:互聯網
上載者:User

這個是根據前面別人的例子改了一下的,呵呵~~~

註:這裡面的System.Data.dll 是Unity\Editor\Data\Mono\lib\mono\2.0\System.Data.dll;但是MySql.data.dll的版本我也不知道怎麼來判斷,我本來用的是1.0.6.15336這個版本的,但是一直報錯“MySqlException: Character set 'gbk' is not supported”,報這個錯誤一般都是MySql.data.dll的版本太低了,後來我改成5.0的了,結果就ok了。反正我傳到我的資源裡面了,不行的話你們一個個試。

資料庫建立如下:    

set feedback off;drop database db;create database db;USE DB;drop table s;create table s(    sno Int(10) not null primary key,    name varchar(10),    sex varchar(10)  );insert into s values('001','楊純','男');insert into s values('002','yaya','女');insert into s values('003','dlnuchunge','男');select * from s;

unity3d裡面的代碼如下:

using UnityEngine;using System;using System.Collections;using System.Data;using MySql.Data.MySqlClient;public class CMySql : MonoBehaviour {    // Global variables    public static MySqlConnection dbConnection;//Just like MyConn.conn in StoryTools before     static string host = "localhost";     static string id = "root";     static string pwd = "mysql";     static string database = "db";     static string result = "";    private string strCommand = "Select sno from s ;";public static DataSet MyObj;     void OnGUI()     {         host = GUILayout.TextField( host, 200, GUILayout.Width(200));         id = GUILayout.TextField( id, 200, GUILayout.Width(200));         pwd = GUILayout.TextField( pwd, 200, GUILayout.Width(200));         if(GUILayout.Button("Test"))         {    string connectionString = string.Format("Server = {0}; Database = {1}; User ID = {2}; Password = {3};",host,database,id,pwd);    openSqlConnection(connectionString);     MySqlCommand mySqlCommand = new MySqlCommand("Select * from s;", dbConnection); MySqlDataReader reader = mySqlCommand.ExecuteReader();        try        {            while (reader.Read())            {                if (reader.HasRows)                {                    print("編號:" + reader.GetInt32(0)+"|姓名:"+reader.GetString(1)+"|性別:"+reader.GetString(2));                }            }        }        catch (Exception)        {            Console.WriteLine("查詢失敗了!");        }        finally        {            reader.Close();        }    MyObj = GetDataSet(strCommand);         }          GUILayout.Label(result);     }      // On quit    public static void OnApplicationQuit() {        closeSqlConnection();    }       // Connect to database    private static void openSqlConnection(string connectionString) {        dbConnection = new MySqlConnection(connectionString);        dbConnection.Open();        result = dbConnection.ServerVersion;        //Debug.Log("Connected to database."+result);    }       // Disconnect from database    private static void closeSqlConnection() {        dbConnection.Close();        dbConnection = null;        //Debug.Log("Disconnected from database."+result);    }        // MySQL Query    public static void doQuery(string sqlQuery) {        IDbCommand dbCommand = dbConnection.CreateCommand();            dbCommand.CommandText = sqlQuery;        IDataReader reader = dbCommand.ExecuteReader();        reader.Close();        reader = null;        dbCommand.Dispose();        dbCommand = null;    }    #region Get DataSet    public  DataSet GetDataSet(string sqlString)    {        //string sql = UnicodeAndANSI.UnicodeAndANSI.UnicodeToUtf8(sqlString);           DataSet ds = new DataSet();        try        {            MySqlDataAdapter da = new MySqlDataAdapter(sqlString, dbConnection);            da.Fill(ds);           }        catch (Exception ee)        {               throw new Exception("SQL:" + sqlString + "\n" + ee.Message.ToString());        }        return ds;      }    #endregion }

結果如下:

呵呵,大家試試吧~~~~

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.