MySQL 学习笔记(三) C# 调用MySQL数据库

MySQL提供了很多的连接器和API来帮助在不同平台的使用,具体可以查看官方文档Chapter 27 Connectors and APIs
本文的环境设置:Win7, MySQL 5.7.13, Visual Studio 2015.

安装 Connector/Net

首先下载MySQL的Net连接器。
下载地址为:https://dev.mysql.com/downloads/file/?id=463758
安装后默认地址为:C:\Program Files (x86)\MySQL\MySQL Connector Net 6.9.9\Assemblies ,目录下有两个版本文件夹对应VS不同的版本V4.0(VS2010) 和 V4.5(VS2012/2013/2015),我选择4.5. 可以在Documentation中发现帮助手册 ConnectorNET.chm。

C#中添加MySql.Data.dll引用

新建一个窗体项目。

添加引用V4.5 文件夹下的MySql.Data.dll

MySqlCommand 类: 代表对MySQL数据库进行执行操作的SQL语句。
MySqlDataAdapter 类: 代表一组数据命令和数据库连接,用于填充数据库和更新MySQL数据库。
MySqlConnection 类: 代表与MySQL服务器数据库的开放式连接。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
public partial class Form1 : Form {

public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
string connStr = "server=localhost;user=root;database = test;port=3306;password=123456789!;";
MySqlConnection conn = new MySqlConnection(connStr);

try {
label1.Text = "Connecting to MySQL......";
Console.WriteLine("Connecting to MySQL......");
conn.Open();

string sql = "select * from student";
MySqlCommand cmd = new MySqlCommand(sql,conn);

MySqlDataAdapter da = new MySqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
dataGridView1.DataSource = ds.Tables[0];
conn.Close();


} catch(Exception ex) {
Console.WriteLine(ex);
}
}
}

运行结果:

C#中添加更新和删除功能

首先如果要对一个表进行更新修改,这个表一定要有主键。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
MySqlDataAdapter da;
private void button1_Click(object sender, EventArgs e)
{
string connStr = string.Format("server=localhost;user={0};database=test;port=3306;password={1};" ,textBox1.Text.Trim(),textBox2.Text.Trim());
MySqlConnection conn = new MySqlConnection(connStr);
// 连接到数据库
try {
conn.Open();

string sql = "select * from student";
MySqlCommand comm = new MySqlCommand(sql,conn);

da = new MySqlDataAdapter(comm);
//DataSet ds = new DataSet();
DataTable dt = new DataTable();
// da.Fill(ds);
da.Fill(dt);
//dataGridView1.DataSource = ds.Tables[0];
dataGridView1.DataSource = dt;
}
catch (Exception ex)
{
MessageBox.Show("connection failed.");
}

}

public void updateSql() {

//更新功能
try {
DataTable dt = (DataTable)dataGridView1.DataSource;
MySqlCommandBuilder cmBuilder = new MySqlCommandBuilder(da);
da.Update(dt);
MessageBox.Show("update succeed.");
}
catch (Exception ex)
{
MessageBox.Show("update failed.");
}
}

public void deleteSql() {

// 删除功能
int select_row = dataGridView1.SelectedRows.Count;
int select_cell = dataGridView1.SelectedCells.Count;
if (select_cell>0) {
if (select_row>0) {
foreach (DataGridViewRow drow in dataGridView1.SelectedRows) {
dataGridView1.Rows.Remove(drow);
}
MessageBox.Show("Delete "+select_row+"rows.","Success");
}
if (dataGridView1.SelectedCells.Count>0) {
ArrayList al = new ArrayList();
foreach (DataGridViewCell dcell in dataGridView1.SelectedCells) {
if (al.IndexOf(dcell.RowIndex)==-1) {
al.Add(dcell.RowIndex);
}
}

foreach (int number in al) {
dataGridView1.Rows.RemoveAt(number);
}

}

}

}

private void button2_Click(object sender, EventArgs e) //更新按钮
{
updateSql();

}

private void button3_Click(object sender, EventArgs e) //删除按钮
{
deleteSql();
updateSql();
}

}



[1]. 参考: https://dev.mysql.com/doc/refman/5.7/en/entering-queries.html
[2]. 参考: http://blog.csdn.net/liyuqian199695/article/details/53556639

如果觉得对您有帮助,就扫我交个朋友吧!