2015-12-03 205 views
2

我試圖讓Unity客戶端建立與MySQL服務器的連接,純粹是爲了讀取。連接時出現錯誤。Unity3D與MySQL的連接錯誤

我的代碼:

using UnityEngine; 
using System; 
using System.Data; 
using System.Text; 
using System.Collections; 
using System.Collections.Generic; 
using System.Security.Cryptography; 
using MySql.Data; 
using MySql.Data.MySqlClient; 

public class DatabaseHandler : MonoBehaviour { 

    public string host, database, user, password; 
    public bool pooling = true; 

    private string connectionString; 
    private MySqlConnection con = null; 
    private MySqlCommand cmd = null; 
    private MySqlDataReader rdr = null; 

    private MD5 _md5Hash; 

    void Awake() { 

     DontDestroyOnLoad(this.gameObject); 
     connectionString = "Server="+host+";Database="+database+";User ID="+user+";Password="+password+";Pooling="; 
     if (pooling){ 
      connectionString += "true;"; 
     } else { 
      connectionString += "false;"; 
     } 

     try { 
      con = new MySqlConnection(connectionString); 
      con.Open(); // THIS is line 47 in the error 
      Debug.Log ("Mysql State: " + con.State); 
     } catch (Exception e) { 
      Debug.Log (e); // THIS is line 51 in the error 
     } 

    } 
} 

錯誤:

System.Collections.Generic.KeyNotFoundException: The given key was not present in the dictionary. at System.Collections.Generic.Dictionary`2[System.String,MySql.Data.MySqlClient.CharacterSet].get_Item (System.String key) [0x000a2] in /Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System.Collections.Generic/Dictionary.cs:150 at MySql.Data.MySqlClient.CharSetMap.GetCharacterSet (DBVersion version, System.String CharSetName) [0x00000] in :0 at MySql.Data.MySqlClient.CharSetMap.GetEncoding (DBVersion version, System.String CharSetName) [0x00000] in :0 at MySql.Data.MySqlClient.Driver.Configure (MySql.Data.MySqlClient.MySqlConnection connection) [0x00000] in :0 at MySql.Data.MySqlClient.MySqlConnection.Open () [0x00000] in :0 at (wrapper remoting-invoke-with-check) MySql.Data.MySqlClient.MySqlConnection:Open() at DatabaseHandler.Awake() [0x0007a] in /Users/ThaMacPro/Desktop/Unity Projects/youtubeMySQL/Assets/Scripts/DatabaseHandler.cs:47 UnityEngine.Debug:Log(Object) DatabaseHandler:Awake() (at Assets/Scripts/DatabaseHandler.cs:51)

誰能告訴我這個錯誤怎麼能解決嗎?

謝謝!

+0

看起來像你提供了一個無效的連接字符串;忘記統一,嘗試谷歌搜索這個特定的mysql錯誤。 – Roberto

+0

謝謝@Roberto,在搜索了更具體的關鍵字之後,我找到了解決這個問題的方法。 http://stackoverflow.com/questions/25933522/why-it-doesnt-work-when-i-use-mysqlconnection-open – wsgb

+0

你想提供該鏈接作爲答案?我會將它標記爲答案@Roberto – wsgb

回答

0

解決方案:Why it doesn't work when I use MysqlConnection.open()?

只好字符集添加到connnectionString。

謝謝@Roberto! :-)

+0

我遇到了大多數人沒有遇到的同類錯誤的更多問題,所以我最終爲我的MySQL數據庫使用了不同的主機(rackspace)。沒有更多的問題。 – wsgb