<Unity3D>unity连接数据库MySQL

  1. 运行环境:Win7 64位,Unity3.5.0,MySQL5.5

  2. 在Unity新建一个场景,保存并命名为Unity_MySQL。

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

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

    using UnityEngine;  
    using System;  
    using System.Collections;  
    using System.Data;  
    using MySql.Data.MySqlClient;  
    
    public class CMySql : MonoBehaviour {  
         public static MySqlConnection dbConnection;//Just like MyConn.conn in StoryTools before  
         static string host = "127.0.0.1";  
         static string id = "root";  //***不要变***
         static string pwd = "zym123";  //密码
         static string database = "unity";//数据库名  
         static string result = "";  
          
         private string strCommand = "Select ID from unity ;";  
         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);    
                MyObj = GetDataSet(strCommand);
    
                //读取数据函数
                ReaderData();
    
              }   
               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;  //获得MySql的版本
        }  
         
        // Disconnect from database  
        private static void closeSqlConnection() 
        {  
            dbConnection.Close();  
            dbConnection = null;  
        }  
          
        // 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)  
        {   
            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   
    
        //读取数据函数
        void ReaderData()
        {
            MySqlCommand mySqlCommand = new MySqlCommand("Select * from unity;", dbConnection);
            MySqlDataReader reader = mySqlCommand.ExecuteReader();
            try
            {
                while (reader.Read())
                {
                    if (reader.HasRows)
                    {
                        print("ID:" + reader.GetInt32(0) + "--Name:" + reader.GetString(1) + "--Sex:" + reader.GetString(2));
                    }
                }
            }
            catch (Exception)
            {
                Console.WriteLine("查询失败了!");
            }
            finally
            {
                reader.Close();
            }         
        }
    }  
    

     

  5. 这个脚本中引用了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文件夹下。并在VS2010中引用。

  6. (Unity中的资源)(vs2010中的资源)

  7. 启动MySQL。

  8. 创建一个数据库,命名为unity,并在其中新建一张表,也命名为unity。下图是表的设计视图。

  9. 在表中添加几条记录。(大家自己添加了~)

  10. 在Unity中将CMySql脚本拖放到Main Camera上去。运行Unity~运行界面如下:

  11. 点击Test按钮。运行结果如下:

  12. 并且我们在Game窗口可以看到我们使用的MySql的版本。

  13. 项目文件:在我个人资源中有下载。

Rebuild started: Project: Project *** Using Compiler &#39;V6.22&#39;, folder: &#39;E:\Keil_v5\ARM\ARMCLANG\Bin&#39; Rebuild target &#39;Target 1&#39; assembling startup_stm32f10x_md.s... Start/core_cm3.c(445): error: non-ASM statement in naked function is not supported 445 | uint32_t result=0; | ^ Start/core_cm3.c(442): note: attribute is here 442 | uint32_t __get_PSP(void) __attribute__( ( naked ) ); | ^ Start/core_cm3.c(465): error: parameter references not allowed in naked functions 465 | "BX lr \n\t" : : "r" (topOfProcStack) ); | ^ Start/core_cm3.c(461): note: attribute is here 461 | void __set_PSP(uint32_t topOfProcStack) __attribute__( ( naked ) ); | ^ Start/core_cm3.c(479): error: non-ASM statement in naked function is not supported 479 | uint32_t result=0; | ^ Start/core_cm3.c(476): note: attribute is here 476 | uint32_t __get_MSP(void) __attribute__( ( naked ) ); | ^ Start/core_cm3.c(499): error: parameter references not allowed in naked functions 499 | "BX lr \n\t" : : "r" (topOfMainStack) ); | ^ Start/core_cm3.c(495): note: attribute is here 495 | void __set_MSP(uint32_t topOfMainStack) __attribute__( ( naked ) ); | ^ 4 errors generated. compiling core_cm3.c... compiling misc.c... compiling system_stm32f10x.c... compiling stm32f10x_adc.c... compiling stm32f10x_dac.c... compiling stm32f10x_exti.c... compiling stm32f10x_dbgmcu.c... compiling stm32f10x_dma.c... compiling stm32f10x_crc.c... compiling stm32f10x_cec.c... compiling stm32f10x_bkp.c... compiling stm32f10x_can.c... compiling stm32f10x_flash.c... compiling stm32f10x_pwr.c... compiling stm32f10x_fsmc.c... compiling stm32f10x_
03-31
评论 13
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值