VS2017C#连接MySql

首先我们去MySQL官网下载MySQL Connector/Net,根据需要下载32位或64位。
下载地址:https://dev.mysql.com/downloads/connector/net
下载完成后直接安装即可,需要注意的是,不同的MySQL Connector/Net版本支持的.net framework版本也是不同的,比如MySQL Connector/Net 6.9.9及之前的版本支持.net framework 4.0,而MySQL Connector/Ne 6.10.1及以后的版本需要.net framework 4.5.2或更高版本才能支持。所以我用的是高版本.Net Framework 4.6.1;

安装完MySql Connector/Net 后,找到MySql.Data.dll类库;
然后在VS2017中打开我们的项目,添加对 MySql.Data.dll的引用,如下图所示
VS2017C#连接MySql

添加成功后,在项目中引入命名空间:

using MySql.Data.MySqlClient;

加入测试代码

public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string connString = "server=192.168.22.58;database=tomato_dev;uid=tomato;pwd=tomato";//数据连接字段
            MySqlConnection conn = new MySqlConnection(connString);//之前SQLServer的连接名是SqlConnection
            try
            {
                conn.Open();
                MessageBox.Show("连接成功!");
            }
            catch(MySqlException ex)
            {
                MessageBox.Show(ex.Message);

            }
            finally
            {
                conn.Close();
            }
        }
    }

VS2017C#连接MySql

小结:做好平时的点滴积累,相信这些都会在以后的工作中提供帮助;