多米诺 发表于 2012-6-15 12:31

Unity3d 连接MySQL数据库代码

C#代码:view plaincopy to clipboardprint?
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 = "192.168.1.100";   
   static string id = "mysql";//这里是你自己的数据库的用户名字,我一开始想用root,发现不行,后来添加了新的用户才可以   
   static string pwd = "123456";   
   static string database = "test";   
   static string result = "";   
      
private string strCommand = "Select * from unity3d_test ORDER BY id;";   
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);   
         }   
         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   
    publicDataSet 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   
} 此段代码网络上出现在很多不同的地方,已经搞不清是出自哪位高人之手了,所以在感激的同时感到抱歉无法注明出处了,希望更多的人能够从中受益。



另一段C#代码:using UnityEngine;   
using System;   
using System.Collections;   
using System.Data;   
public class DataBaseTest : MonoBehaviour {   
public GUISkin myGUISkin = new GUISkin();   
string strID = "";   
string strName = "";   
string strSex = "";   
int Index = 1;   
// Use this for initialization   
void Start () {   
}   
void OnGUI()   
{   
GUI.skin = myGUISkin;   
if (GUI.Button(new Rect(100,320,100,100),"Click Me"))   
{   
   foreach(DataRow dr in CMySql.MyObj.Tables.Rows)   
   {   
    if (Index.ToString() == dr["ID"].ToString())   
    {   
   strID = dr["ID"].ToString();   
   strName =dr["Name"].ToString();   
   strSex = dr["Sex"].ToString();   
      
   break;   
    }   
   }      
   Index++;   
    if(Index > 5)   
   {   
    Index = 1;   
   }   
      
}   
GUI.Label(new Rect(320,100,150,70),"DataBaseTest");   
GUI.Label(new Rect(300,210,150,70),strID);   
GUI.Label(new Rect(300,320,150,70),strName);   
GUI.Label(new Rect(300,430,150,70),strSex);   
   
}   
}

lovehou922 发表于 2017-2-15 13:40

很不错

gaojinjin 发表于 2017-2-15 13:15

顶顶多好

yaotaiye 发表于 2017-2-15 13:02

真心顶

chris 发表于 2017-2-15 13:17

难得一见的好帖

yaotaiye 发表于 2017-2-15 13:08

说的非常好

chenlu632 发表于 2017-3-3 20:19

很不错

隐忍的微笑 发表于 2017-3-3 20:28

好帖就是要顶

Invasion 发表于 2017-3-3 20:11

顶顶多好

cheangerlove 发表于 2017-3-3 20:20

难得一见的好帖
页: [1] 2 3 4 5 6 7 8 9
查看完整版本: Unity3d 连接MySQL数据库代码