怎么用net打开或关闭mysql

MySQL是一个流行的关系型数据库,通过.NET可以很方便地打开和关闭它。下面就来介绍一下具体的方法。

//连接字符串 string connStr = "server=localhost;port=3306;database=mydb;uid=root;pwd=123456;"; //打开连接 MySqlConnection conn = new MySqlConnection(connStr); try { conn.Open(); Console.WriteLine("连接已打开."); } catch (Exception ex) { Console.WriteLine("连接打开失败:" + ex.Message); } //关闭连接 try { conn.Close(); Console.WriteLine("连接已关闭."); } catch (Exception ex) { Console.WriteLine("连接关闭失败:" + ex.Message); }