查看: 1483|回复: 4
打印 上一主题 下一主题

[经验分享] 连接MySQL数据库测试成功_教程

[复制链接]

2508

主题

2

听众

3万

积分

资深设计师

Rank: 7Rank: 7Rank: 7

纳金币
32806
精华
12

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

跳转到指定楼层
楼主
发表于 2012-6-29 14:20:23 |只看该作者 |倒序浏览


   
        
            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   

                 
                    public  DataSet 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 + "
" + 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[0].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);   

                 
                     

                 
                }   

                 
                }
            
            
            
            
        
   

分享到: QQ好友和群QQ好友和群 腾讯微博腾讯微博 腾讯朋友腾讯朋友 微信微信
转播转播0 分享淘帖0 收藏收藏0 支持支持0 反对反对0
回复

使用道具 举报

2317

主题

54

听众

2万

积分

资深设计师

Rank: 7Rank: 7Rank: 7

纳金币
20645
精华
62

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

沙发
发表于 2012-12-30 04:15:05 |只看该作者
学习了,虽然还是有难度,谢谢楼主的用心
回复

使用道具 举报

nts    

3

主题

1

听众

743

积分

初级设计师

Rank: 3Rank: 3

纳金币
7
精华
0

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

板凳
发表于 2013-10-17 11:12:48 |只看该作者
我觉得用Sqlite才是王道
回复

使用道具 举报

0

主题

1

听众

239

积分

设计实习生

Rank: 2

纳金币
89
精华
0

最佳新人

地板
发表于 2014-6-12 16:45:27 |只看该作者
赞一个。有机会好好研究一下。
回复

使用道具 举报

0

主题

1

听众

93

积分

设计初学者

Rank: 1

纳金币
13
精华
0

活跃会员 灌水之王

5#
发表于 2014-6-14 13:41:07 |只看该作者
不查api完全不懂写的什么
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

关闭

站长推荐上一条 /1 下一条

手机版|纳金网 ( 闽ICP备08008928号

GMT+8, 2024-5-7 00:47 , Processed in 0.090409 second(s), 28 queries .

Powered by Discuz!-创意设计 X2.5

© 2008-2019 Narkii Inc.

回顶部