时间:2021-07-01 10:21:17 帮助过:15人阅读
直接上代码:
using MySQLDriverCS; private void button1_Click(object sender, EventArgs e) { string conn, query = null; DataSet myds; //conn = "Data Source=localhost;User Id=root;Password=123456;Database=test"; //conn = "server=localhost;uid=root;pwd=123456;database=test"; query = "select * from test1"; MySQLConnection sqlconn = new MySQLConnection(conn); MySQLDataAdapter adapter = new MySQLDataAdapter(); sqlconn.Open(); adapter.SelectCommand = new MySQLCommand(query, sqlconn); myds = new DataSet(); adapter.Fill(myds, "test1"); bindingSource1.DataSource = myds.Tables["test1"]; dataGridView1.DataSource = bindingSource1; sqlconn.Close(); }
其中注释掉的两行conn是我参照网上写的两种连接信息,但都不对
尝试第一行conn报错: MySQLDriverCS Exception: MySQLDriverCS Error: can't connect.Unknown database 'localhost'
尝试第二行conn报错: MySQLDriverCS Exception: MySQLDriverCS Error: can't connect.Access denied for user 'ODBC'@'localhost' (using password: NO)
我的mysql中test数据库test1表都没问题,使用mysql命令行可以正常浏览。
基本上把网上能找到的链接mysql数据源的方法都试过了,都失败了,各位大神看一下,到底是连接信息错误还是我整个方法都有问题,如果有应该怎么修改?谢谢。