找回密码
 立即注册
查看: 6420|回复: 70

[网络] Unity3D连接 MySQL (使用DataSet)

[复制链接]
发表于 2012-6-15 12:49 | 显示全部楼层 |阅读模式
  1. using UnityEngine;
  2. using System;
  3. using System.Collections;
  4. using System.Data;
  5. using MySql.Data.MySqlClient;
  6. public class CMySql : MonoBehaviour {
  7.     // Global variables
  8.     public static MySqlConnection dbConnection;//Just like MyConn.conn in StoryTools before

  9.      static string host = "192.168.1.100";
  10.      static string id = "mysql";
  11.      static string pwd = "123456";
  12.      static string database = "test";
  13.      static string result = "";
  14.    
  15. private string strCommand = "Select * from unity3d_test ORDER BY id;";
  16. public static DataSet MyObj;
  17.      void OnGUI()
  18.      {
  19.          host = GUILayout.TextField( host, 200, GUILayout.Width(200));
  20.          id = GUILayout.TextField( id, 200, GUILayout.Width(200));
  21.          pwd = GUILayout.TextField( pwd, 200, GUILayout.Width(200));
  22.          if(GUILayout.Button("Test"))
  23.          {
  24.     string connectionString = string.Format("Server = {0}; Database = {1}; User ID = {2}; Password = {3};",host,database,id,pwd);
  25.     openSqlConnection(connectionString);
  26.    
  27.     MyObj = GetDataSet(strCommand);
  28.          }
  29.          GUILayout.Label(result);
  30.      }  
  31.     // On quit
  32.     public static void OnApplicationQuit() {
  33.         closeSqlConnection();
  34.     }
  35.    
  36.     // Connect to database
  37.     private static void openSqlConnection(string connectionString) {
  38.         dbConnection = new MySqlConnection(connectionString);
  39.         dbConnection.Open();
  40.         result = dbConnection.ServerVersion;
  41.         //Debug.Log("Connected to database."+result);
  42.     }
  43.    
  44.     // Disconnect from database
  45.     private static void closeSqlConnection() {
  46.         dbConnection.Close();
  47.         dbConnection = null;
  48.         //Debug.Log("Disconnected from database."+result);
  49.     }
  50.    
  51.     // MySQL Query
  52.     public static void doQuery(string sqlQuery) {
  53.         IDbCommand dbCommand = dbConnection.CreateCommand();   
  54.         dbCommand.CommandText = sqlQuery;
  55.         IDataReader reader = dbCommand.ExecuteReader();
  56.         reader.Close();
  57.         reader = null;
  58.         dbCommand.Dispose();
  59.         dbCommand = null;
  60.     }
  61.     #region Get DataSet
  62.     public  DataSet GetDataSet(string sqlString)
  63.     {
  64.         //string sql = UnicodeAndANSI.UnicodeAndANSI.UnicodeToUtf8(sqlString);
  65.   
  66.   
  67.   DataSet ds = new DataSet();
  68.         try
  69.         {
  70.             MySqlDataAdapter da = new MySqlDataAdapter(sqlString, dbConnection);
  71.             da.Fill(ds);
  72.    
  73.         }
  74.         catch (Exception ee)
  75.         {
  76.    
  77.             throw new Exception("SQL:" + sqlString + "\n" + ee.Message.ToString());
  78.         }
  79.         return ds;
  80.   
  81.     }
  82.     #endregion
  83. }
复制代码
/*
...本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/nette/archive/2009/07/30/4394849.aspx
*/

  1. using UnityEngine;
  2. using System;
  3. using System.Collections;
  4. using System.Data;

  5. public class DataBaseTest : MonoBehaviour {
  6. public GUISkin myGUISkin = new GUISkin();
  7. string strID = "";
  8. string strName = "";
  9. string strSex = "";
  10. int Index = 1;
  11. // Use this for initialization
  12. void Start () {
  13. }

  14. void OnGUI()
  15. {
  16.   GUI.skin = myGUISkin;
  17.   if (GUI.Button(new Rect(100,320,100,100),"Click Me"))
  18.   {
  19.    foreach(DataRow dr in CMySql.MyObj.Tables[0].Rows)
  20.    {
  21.     if (Index.ToString() == dr["ID"].ToString())
  22.     {
  23.      strID = dr["ID"].ToString();
  24.      strName =  dr["Name"].ToString();
  25.      strSex = dr["Sex"].ToString();
  26.      
  27.      break;
  28.     }
  29.    }   
  30.    Index++;
  31.     if(Index > 5)
  32.    {
  33.     Index = 1;
  34.    }  
  35.    
  36.   }
  37.   GUI.Label(new Rect(320,100,150,70),"DataBaseTest");
  38.   GUI.Label(new Rect(300,210,150,70),strID);
  39.   GUI.Label(new Rect(300,320,150,70),strName);
  40.   GUI.Label(new Rect(300,430,150,70),strSex);
  41.   
  42. }
  43. }
复制代码
2.導入dll
  同先前的帖子 , 將MySql.data.dll Import至Assets底下 , 然後再到Unity\Editor\Data\Frameworks\Mono.framework 中
將System.Data.dll 也一起Import至Assets內 , 當然 , 如果想顯示中文的話 , 請參考中文視頻教學 , 建立一個GUISkin與字型

3.建立數據庫內容
  主要是因為代碼中的這段內容
  1. static string host = "192.168.1.100";
  2.      static string id = "mysql";
  3.      static string pwd = "123456";
  4.      static string database = "test";
  5.      private string strCommand = "Select * from unity3d_test ORDER BY id;";
复制代码
其中host ,id , pwd 請自行設定 , 簡單的說就是連進你的MySQL啦~
然後建立一個名為test的Database , 在這個test下建立一張table , 取名為 unity3d_test ,
接下來就為這張unity3d_test建立3個欄位 : ID , Name , Sex (記得將ID設定為primary key 且默認值為1)
再來自行填入5筆資料(5筆資料的原因是腳本那邊是設定成5筆資料一個循環 , 使用者可以自行更改腳本試試)

4.建立GameObject
  建立完GameObject後將上面兩個腳本掛上去 , 如果有建立GUISkin , 記得指定GUISkin

5.執行
  執行後先按Test按鈕來連接數據庫 , 然後再按"Click Me"來顯示數據庫內的內容

6.總結
  以上部份是有關連線至數據庫後 , 再將資料用DataSet的方式獲得出來再加以顯示至介面上 .




发表于 2017-4-2 07:43 | 显示全部楼层
很不错
发表于 2017-4-2 08:09 | 显示全部楼层
好帖就是要顶
发表于 2017-4-2 07:59 | 显示全部楼层
说的非常好
发表于 2017-4-2 07:23 | 显示全部楼层
很好哦
发表于 2017-4-2 08:04 | 显示全部楼层
不错不错
发表于 2017-5-17 13:47 | 显示全部楼层
很不错
发表于 2017-5-17 14:15 | 显示全部楼层
楼主是超人
发表于 2017-5-17 14:40 | 显示全部楼层
顶顶多好
发表于 2017-5-17 14:12 | 显示全部楼层
说的非常好
懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Unity开发者联盟 ( 粤ICP备20003399号 )

GMT+8, 2024-5-1 12:55 , Processed in 0.100425 second(s), 26 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表