static class Program { /// <summary> /// 应用程序的主入口点。 /// </summary> [STAThread] static void Main() { // String mysqlStr = "server = localhost; user id=root; database=mysql; port=3306; password=1"; String mysqlStr = "Database=test;Data Source=127.0.0.1;User Id=root;Password=1;pooling=false;CharSet=utf8;port=3306"; MySqlConnection mycon = new MySqlConnection(mysqlStr); mycon.Open(); MySqlCommand command = new MySqlCommand("show databases"); MySqlDataReader reader = command.ExecuteReader();//走到这行报错 bool b = reader.HasRows; if (reader.HasRows) { MessageBox.Show("数据插入成功!"); } mycon.Close(); Application.Run(new Form1()); } }
解决方案
20
mycon.Open();
MySqlCommand command = new MySqlCommand(“show databases”);
MySqlDataReader reader = command.ExecuteReader();//走到这行报错
问一下command 在哪里跟mycon关联了?快快添加代码。
string mysql = "SELECT * from users"; //创建数据库连接 MySqlConnection myconnection = new MySqlConnection(myconn); myconnection.Open(); //创建MySqlCommand对象 MySqlCommand mycommand = new MySqlCommand(mysql, myconnection); //通过MySqlCommand的ExecuteReader()方法构造DataReader对象 MySqlDataReader myreader = mycommand.ExecuteReader();