第一步,添加MySql.Data工具,
首先,C#連接MySql數據庫需要用到C#連接MySql數據庫所用到的動態鏈接庫--MySql.Data,如果沒有這個文件首先我們需要將他添加進項目中,
1.右鍵項目名,點擊管理NuGet程序包:

2.在瀏覽頁面的搜索欄輸入MySql.Data,如果沒有安裝右側會有安裝一欄選項,我們就可以點擊右側的安裝選項進行安裝,安裝成功后我們就可以進行編碼操作了:

第二步,編碼實現,
然后,我們就可以進入編碼階段了,
首先我們需要加入頭文件:
using MySql.Data.MySqlClient;
這樣我們就可以使用MySql.Data中的方法來連接數據庫了,連接數據庫代碼如下:
String connetStr = "server=127.0.0.1;port=3306;user=root;password=123; database=vs;"; //usr:用戶名,password:數據庫密碼,database:數據庫名 MySqlConnection conn = new MySqlConnection(connetStr); try { conn.Open();//打開通道,建立連接,可能出現異常,使用try catch語句 Console.WriteLine("已經建立連接"); } catch (MySqlException ex) { Console.WriteLine(ex.Message); } finally { conn.Close(); }
如果連接數據庫成功,我們就可以進行下面的操作了,取出數據並通過DataGridView展示出來了,代碼如下:
String connetStr = "server=127.0.0.1;port=3306;user=root;password=123; database=vs;"; MySqlConnection conn = new MySqlConnection(connetStr); try { conn.Open();//打開通道,建立連接,可能出現異常,使用try catch語句 Console.WriteLine("已經建立連接");
string sql = "select * from salecar"; MySqlCommand cmd = new MySqlCommand(sql, conn); MySqlDataReader reader = cmd.ExecuteReader();//執行ExecuteReader()返回一個MySqlDataReader對象 while (reader.Read()) { int index = this.dataGridView1.Rows.Add(); this.dataGridView1.Rows[index].Cells[0].Value = reader.GetString("name"); this.dataGridView1.Rows[index].Cells[1].Value = reader.GetString("describe"); this.dataGridView1.Rows[index].Cells[2].Value = reader.GetString("price"); this.dataGridView1.Rows[index].Cells[3].Value = reader.GetInt32("salenumber"); } } catch (MySqlException ex) { Console.WriteLine(ex.Message); } finally { conn.Close(); }
這樣我們就完成了C#窗體連接MySql並通過DataGridView展示數據,下面是效果圖和全部代碼:
全部代碼:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using MySql.Data.MySqlClient; namespace WindowsFormsApp1 { public partial class Form3 : Form { public Form3() { InitializeComponent(); a(); } private void button1_Click(object sender, EventArgs e) { Form1 fm1 = new Form1(); this.Hide(); fm1.ShowDialog(); Application.ExitThread(); } private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e) { } public void a() { String connetStr = "server=127.0.0.1;port=3306;user=root;password=123; database=vs;"; MySqlConnection conn = new MySqlConnection(connetStr); try { conn.Open();//打開通道,建立連接,可能出現異常,使用try catch語句 Console.WriteLine("已經建立連接"); //在這里使用代碼對數據庫進行增刪查改 string sql = "select * from salecar"; MySqlCommand cmd = new MySqlCommand(sql, conn); MySqlDataReader reader = cmd.ExecuteReader();//執行ExecuteReader()返回一個MySqlDataReader對象 while (reader.Read()) { int index = this.dataGridView1.Rows.Add(); this.dataGridView1.Rows[index].Cells[0].Value = reader.GetString("name"); this.dataGridView1.Rows[index].Cells[1].Value = reader.GetString("describe"); this.dataGridView1.Rows[index].Cells[2].Value = reader.GetString("price"); this.dataGridView1.Rows[index].Cells[3].Value = reader.GetInt32("salenumber"); } } catch (MySqlException ex) { Console.WriteLine(ex.Message); } finally { conn.Close(); } } } }
效果:
數據庫表:

