Unity3D connection to MySQL error

三世轮回 提交于 2019-12-12 18:18:44

问题


I'm trying to get Unity client to establish connection to MySQL server purely for reads. I get an error when the connection is being made.

My Code:

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
        }

    }
}

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)

Can anyone tell me how this error can be fixed?

Thank you!


回答1:


Solution: Why it doesn't work when I use MysqlConnection.open()?

Had to add the charset to the connnectionString.

Thank you @Roberto! :-)



来源:https://stackoverflow.com/questions/34057309/unity3d-connection-to-mysql-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!