https://blog.csdn.net/qq_42462109/article/details/83385658
参考这篇博文做MySql连接测试,发现一直报“The given key was not present in the dictionary”的错误,
后来查了半天资料结果是因为连接字符串未定义字符编码格式,把charset=utf8添加到原连接字符串后,问题就解决了
如下
/// <summary>
/// 打开数据库
/// </summary>
public void OpenSql()
{
try
{
string mySqlString = string.Format("Database={0};Data Source={1};User Id={2};Password={3};port={4};charset=utf8", databaseName, host, userName, password, port);
mySqlConnection = new MySqlConnection(mySqlString);
//UnityEngine.Debug.Log("连接前" + mySqlConnection.State);
mySqlConnection.Open();
//UnityEngine.Debug.Log("服务器连接成功!");
// UnityEngine.Debug.Log("连接后" + mySqlConnection.State);
}
catch (Exception e)
{
throw new Exception("服务器连接失败,报错:" + e.Message.ToString());
}
}