unity连接数据库MySQL简单例子

来源:互联网 发布:曲子龙 网络尖刀 编辑:程序博客网 时间:2024/06/15 15:03
  1. 在Unity新建一个场景,保存并命名为Unity_MySQL。

  2. 新建一个C#Script,命名为CMySql.cs。

  3. 下面是CMySql.cs脚本的内容:

    [csharp] view plaincopy
    1. using UnityEngine;    
    2. using System;    
    3. using System.Collections;    
    4. using System.Data;    
    5. using MySql.Data.MySqlClient;    
    6.   
    7. public class CMySql : MonoBehaviour {    
    8.      public static MySqlConnection dbConnection;//Just like MyConn.conn in StoryTools before    
    9.      static string host = "127.0.0.1";    
    10.      static string id = "root";  //***不要变***  
    11.      static string pwd = "zym123";  //密码  
    12.      static string database = "unity";//数据库名    
    13.      static string result = "";    
    14.         
    15.      private string strCommand = "Select ID from unity ;";    
    16.      public static DataSet MyObj;    
    17.   
    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.             MyObj = GetDataSet(strCommand);  
    28.   
    29.             //读取数据函数  
    30.             ReaderData();  
    31.   
    32.           }     
    33.            GUILayout.Label(result);    
    34.        }    
    35.     
    36.     // On quit    
    37.     public static void OnApplicationQuit()   
    38.     {    
    39.         closeSqlConnection();    
    40.     }    
    41.        
    42.     // Connect to database    
    43.     private static void openSqlConnection(string connectionString)   
    44.     {    
    45.         dbConnection = new MySqlConnection(connectionString);    
    46.         dbConnection.Open();    
    47.         result = dbConnection.ServerVersion;  //获得MySql的版本  
    48.     }    
    49.        
    50.     // Disconnect from database    
    51.     private static void closeSqlConnection()   
    52.     {    
    53.         dbConnection.Close();    
    54.         dbConnection = null;    
    55.     }    
    56.         
    57.     // MySQL Query    
    58.     public static void doQuery(string sqlQuery)   
    59.     {    
    60.         IDbCommand dbCommand = dbConnection.CreateCommand();        
    61.         dbCommand.CommandText = sqlQuery;    
    62.         IDataReader reader = dbCommand.ExecuteReader();    
    63.         reader.Close();    
    64.         reader = null;    
    65.        dbCommand.Dispose();    
    66.         dbCommand = null;    
    67.     }    
    68.     #region Get DataSet    
    69.     public  DataSet GetDataSet(string sqlString)    
    70.     {     
    71.         DataSet ds = new DataSet();    
    72.         try    
    73.         {    
    74.             MySqlDataAdapter da = new MySqlDataAdapter(sqlString, dbConnection);    
    75.             da.Fill(ds);    
    76.        
    77.         }    
    78.         catch (Exception ee)    
    79.         {         
    80.             throw new Exception("SQL:" + sqlString + "\n" + ee.Message.ToString());    
    81.         }    
    82.         return ds;    
    83.       
    84.     }    
    85.     #endregion     
    86.   
    87.     //读取数据函数  
    88.     void ReaderData()  
    89.     {  
    90.         MySqlCommand mySqlCommand = new MySqlCommand("Select * from unity;", dbConnection);  
    91.         MySqlDataReader reader = mySqlCommand.ExecuteReader();  
    92.         try  
    93.         {  
    94.             while (reader.Read())  
    95.             {  
    96.                 if (reader.HasRows)  
    97.                 {  
    98.                     print("ID:" + reader.GetInt32(0) + "--Name:" + reader.GetString(1) + "--Sex:" + reader.GetString(2));  
    99.                 }  
    100.             }  
    101.         }  
    102.         catch (Exception)  
    103.         {  
    104.             Console.WriteLine("查询失败了!");  
    105.         }  
    106.         finally  
    107.         {  
    108.             reader.Close();  
    109.         }           
    110.     }  
    111. }    
  4. 这个脚本中引用了System.Data.DLL,System.Drawing.DLL和MySql.Data.DLL。其中前两个可以在Unity3D的安装目录下找到:X:\Unity3D\Editor\Data\Mono\lib\mono\2.0\,最后一个MySql.Data.DLL可以从网上下载。将这3个DLL文件放到该项目的Assets文件夹下。

  5. 启动MySQL。

  6. 创建一个数据库,命名为unity,并在其中新建一张表,也命名为unity。

  7. 在表中随便添加几条记录。

  8. 在Unity中将CMySql脚本拖放到Main Camera上去。

  9. 点击运行按钮。

  10. 点击Test按钮。

  11. 在Game窗口可以看到数据库内unity表的几条记录和MySql的版本。

1 0
原创粉丝点击