c# - Unity3D连接MySQL报错

标签 c# mysql dictionary unity3d monodevelop

我试图让 Unity 客户端建立与 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)

谁能告诉我如何修复这个错误?

谢谢!

最佳答案

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

必须将字符集添加到 connnectionString。

谢谢@Roberto! :-)

关于c# - Unity3D连接MySQL报错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34057309/

相关文章:

c# - 如何使用try-catch获取代码的错误行号

c# - 如何捕获自定义文件夹上的 Outlook 约会删除事件?

mysql - 将 dd/mm/yyyy 字符串转换为 MySQL 中的 Unix 时间戳

python - 如何获取二维字典 python 中的所有键

python - 合并附加到字典中类似键的值

c# - 无法将类型 System.LinQ.IOrderedEnumerable 隐式转换为 MyClassCollection。存在隐式转换(您是否缺少强制转换?)

c# - 在 C# 应用程序中接收操作系统级别的按键事件

php - 转换数组取字符串

javascript - 如何将 JSON 数据插入父子表

python - 扫描txt文件,翻译字典中的某些词[Python]