简体   繁体   English

MySql Query / C#上的KeyNotFoundException

[英]KeyNotFoundException on MySql Query / C#

I am developing a software on C#, I must use MySQL connection on this program. 我正在开发一个关于C#的软件,我必须在这个程序上使用MySQL连接。 I can connect to database. 我可以连接到数据库。 Visual Studio is throwing a exception on ExecuteReader() line. Visual Studio在ExecuteReader()行上抛出异常。

Exception: KeyNotFoundException , "The given key was not present in the dictionary"

This is my code: 这是我的代码:

MySqlConnectionStringBuilder bag = new MySqlConnectionStringBuilder();
        bag.Server = "localhost";
        bag.UserID = "root";
        bag.Password = "123456";
        bag.Database = "randevu_takip";
        MySqlConnection baglan = new MySqlConnection();
        baglan.ConnectionString = bag.ToString();
        baglan.Open();

 MySqlCommand komut = new MySqlCommand();
        komut.Connection = baglan;
        komut.CommandType = CommandType.Text;
        komut.CommandText = "SELECT * FROM kullanicilar;";

        MySqlDataReader oku = komut.ExecuteReader();

        while (oku.Read())
        {
            MessageBox.Show(oku.GetString("sifre"));
        }

The problem solved for me when I change my column Collation from utf8mb4_unicode_520_ci to utf8_general_ci. 当我将列Collat​​ion从utf8mb4_unicode_520_ci更改为utf8_general_ci时,问题就解决了。

Try to define same charset for connection and table. 尝试为连接和表定义相同的字符集。

var connection = new MySqlConnection("Server=localhost; Port=3307;  uid=your_name; Pwd=your_password; Database=your_database; charset=utf8;");

To change collation for tables you can use, 要更改可以使用的表的排序规则,

ALTER TABLE mytable CONVERT TO CHARACTER SET utf8

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM