2016-07-02 49 views
2

不知道为什么每次尝试连接时都会收到错误。我尝试了很多东西。当我手动连接时输入错误的密码时,它表示访问未授予或任何我知道它连接,但当它连接时,我得到这个奇怪的错误。由于KeyNotFoundException而无法连接到MySQL数据库

类型 “System.Collections.Generic.KeyNotFoundException”的未处理的异常发生在 mscorlib.dll中其他信息:在 词典中的给定的键不存在。

对于本地主机它有效当我有这个连接字符串。

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

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

例外情况引发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"); 
     } 
    } 
+0

只要我知道,MySQL使用“U​​id and Pwd”作为用户名和密码,而不是连接字符串中的“user和password”。 –

+0

亚我已经尝试过,所以现在失去了: –

+0

试试这个> http://stackoverflow.com/questions/2176329/error-in-mysql-connection-the-given-key-was-not-present-in-the-dictionary错误的连接字符串“pooling = false” –

回答

1

的问题是user=goof场应该是user id=goof

更改连接字符串到这一点:

cn.ConnectionString = "server=db4free.net ;port=3306; USER ID=goof; password=pwd; database=s;"; 
0

尝试使用“用户ID”或“UID”,而不只是“用户”,并让我们知道,如果它做的工作!

+0

没有任何建议奏效。即时通讯很困惑,为什么这不会工作。 –