C#中DBHelper类连接mysql的代码

首先需要安装MySQL Connector/Net,这是一个MySQL的ADO.NET驱动。安装完后就可以很方便地操作MySQL数据库了。

            string connStr = "server=localhost;user id=root; password=mick; database=mysql; pooling=false";

            MySqlConnection myconn = new MySqlConnection(connStr);
            try
            {
                myconn.Open();

                MySqlDataAdapter da = new MySqlDataAdapter("select * from 表名", myconn);
                DataTable dt = new DataTable();
                da.Fill(dt);
                dataGridView1.DataSource = dt;
            }
            catch (MySqlException exc)
            {
                MessageBox.Show(exc.Message);
            }
            finally
            {
                myconn.Close();
            }

你可能感兴趣的:(mysql)