找回密码
 立即注册
查看: 6789|回复: 85

[网络] Unity3d 连接MySQL数据库代码

[复制链接]
发表于 2012-6-15 12:31 | 显示全部楼层 |阅读模式
C#代码:
  1. view plaincopy to clipboardprint?
  2. using UnityEngine;   
  3. using System;   
  4. using System.Collections;   
  5. using System.Data;   
  6. using MySql.Data.MySqlClient;   
  7. public class CMySql : MonoBehaviour {   
  8.     // Global variables   
  9.     public static MySqlConnection dbConnection;//Just like MyConn.conn in StoryTools before   
  10.      static string host = "192.168.1.100";   
  11.      static string id = "mysql";//这里是你自己的数据库的用户名字,我一开始想用root,发现不行,后来添加了新的用户才可以   
  12.      static string pwd = "123456";   
  13.      static string database = "test";   
  14.      static string result = "";   
  15.       
  16. private string strCommand = "Select * from unity3d_test ORDER BY id;";   
  17. public static DataSet MyObj;   
  18.      void OnGUI()   
  19.      {   
  20.          host = GUILayout.TextField( host, 200, GUILayout.Width(200));   
  21.          id = GUILayout.TextField( id, 200, GUILayout.Width(200));   
  22.          pwd = GUILayout.TextField( pwd, 200, GUILayout.Width(200));   
  23.          if(GUILayout.Button("Test"))   
  24.          {   
  25.     string connectionString = string.Format("Server = {0}; Database = {1}; User ID = {2}; Password = {3};",host,database,id,pwd);   
  26.     openSqlConnection(connectionString);   
  27.       
  28.     MyObj = GetDataSet(strCommand);   
  29.          }   
  30.          GUILayout.Label(result);   
  31.      }     
  32.     // On quit   
  33.     public static void OnApplicationQuit() {   
  34.         closeSqlConnection();   
  35.     }   
  36.       
  37.     // Connect to database   
  38.     private static void openSqlConnection(string connectionString) {   
  39.         dbConnection = new MySqlConnection(connectionString);   
  40.         dbConnection.Open();   
  41.         result = dbConnection.ServerVersion;   
  42.         //Debug.Log("Connected to database."+result);   
  43.     }   
  44.       
  45.     // Disconnect from database   
  46.     private static void closeSqlConnection() {   
  47.         dbConnection.Close();   
  48.         dbConnection = null;   
  49.         //Debug.Log("Disconnected from database."+result);   
  50.     }   
  51.       
  52.     // MySQL Query   
  53.     public static void doQuery(string sqlQuery) {   
  54.         IDbCommand dbCommand = dbConnection.CreateCommand();      
  55.         dbCommand.CommandText = sqlQuery;   
  56.         IDataReader reader = dbCommand.ExecuteReader();   
  57.         reader.Close();   
  58.         reader = null;   
  59.         dbCommand.Dispose();   
  60.         dbCommand = null;   
  61.     }  
  62.     #region Get DataSet   
  63.     public  DataSet GetDataSet(string sqlString)   
  64.     {   
  65.         //string sql = UnicodeAndANSI.UnicodeAndANSI.UnicodeToUtf8(sqlString);   
  66.      
  67.      
  68.   DataSet ds = new DataSet();   
  69.         try  
  70.         {   
  71.             MySqlDataAdapter da = new MySqlDataAdapter(sqlString, dbConnection);   
  72.             da.Fill(ds);   
  73.       
  74.         }   
  75.         catch (Exception ee)   
  76.         {   
  77.       
  78.             throw new Exception("SQL:" + sqlString + "\n" + ee.Message.ToString());   
  79.         }   
  80.         return ds;   
  81.      
  82.     }  
  83.     #endregion   
  84. }
复制代码
此段代码网络上出现在很多不同的地方,已经搞不清是出自哪位高人之手了,所以在感激的同时感到抱歉无法注明出处了,希望更多的人能够从中受益。



另一段C#代码:
  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. }
复制代码
发表于 2017-2-15 13:40 | 显示全部楼层
很不错
发表于 2017-2-15 13:15 | 显示全部楼层
顶顶多好
发表于 2017-2-15 13:02 | 显示全部楼层
真心顶
发表于 2017-2-15 13:17 | 显示全部楼层
难得一见的好帖
发表于 2017-2-15 13:08 | 显示全部楼层
说的非常好
发表于 2017-3-3 20:19 | 显示全部楼层
很不错
发表于 2017-3-3 20:28 | 显示全部楼层
好帖就是要顶
发表于 2017-3-3 20:11 | 显示全部楼层
顶顶多好
发表于 2017-3-3 20:20 | 显示全部楼层
难得一见的好帖
懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2024-5-1 15:09 , Processed in 0.132715 second(s), 23 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

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