2

不知道为什么每次尝试连接时都会出错。我尝试了很多东西。当我在手动连接时输入错误的密码时,它说访问权限未授予或其他什么所以我知道它连接但是当它连接时我得到这个奇怪的错误。

mscorlib.dll 中出现“System.Collections.Generic.KeyNotFoundException”类型的未处理异常附加信息:字典中不存在给定键。

对于本地主机,当我有这个连接字符串时它可以工作。

server=localhost; port=3306; USER ID=root; password=***; database=database;

但是当我更改服务器用户名和密码时,它决定不起作用。

异常被抛出cn.Open()

     private void button1_Click(object sender, EventArgs e)
    {
        MySqlConnection cn = new MySqlConnection();
        cn.ConnectionString = "server=db4free.net ;port=3306; 
                               user=goof; password=pwd; database=s;";
        cn.Open();
        MySqlCommand cmd = new MySqlCommand("select * from users where
                                             username = '" 
                                             + textBox1.Text + "',
                                             and password = '" 
                                             + textBox2.Text + "'", cn);
        MySqlDataReader dr;
        dr = cmd.ExecuteReader();
        int count = 0;

        while(dr.Read())
        {
            count += 1;
        }
        if(count == 1)
        {
            MessageBox.Show("success");
        }
        else
        {
            MessageBox.Show("error");
        }
    }
4

2 回答 2

1

The problem is the user=goof field should be user id=goof.

Change your connection string into this:

cn.ConnectionString = "server=db4free.net ;port=3306; USER ID=goof; password=pwd; database=s;";
于 2016-07-02T09:50:09.913 回答
0

尝试使用“user id”或“uid”而不是“user”,让我们知道它是否有效!

于 2016-07-02T10:45:42.533 回答