c#打不开MySQL是咋回事,已经open了咋还说没open

.Net技术 码拜 7年前 (2017-04-17) 1297次浏览
    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());
        }
    }

c#打不开MySQL是咋回事,已经open了咋还说没open

解决方案

20

引用:
Quote: 引用:

c#打不开MySQL是咋回事,已经open了咋还说没openopen是open了,但是你用到了吗?

走到 command.ExecuteReader() 这行就挂了

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();

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明c#打不开MySQL是咋回事,已经open了咋还说没open
喜欢 (0)
[1034331897@qq.com]
分享 (0)