1、安裝驅動
下載下傳位址:http://dev.mysql.com/downloads/connector/net/
預設是安裝在C槽:C:\Program Files\MySQL\MySQL Connector Net 6.9.9
其中,C:\Program Files\MySQL\MySQL Connector Net 6.9.9\Assemblies下有v4.0和v4.5。
(VS2010使用V4.0下的dll檔案
VS2012/2013/2015使用v4.5下的dll檔案)
其中有一個幫助手冊十分有用:
Documentation檔案夾下的ConnectorNET.chm中包含了連接配接mysql資料庫的API。
安裝完後我們選擇使用v4.0。然後在應用工程中引用元件MySql.Data.dll。
2、把MySql.Data.dll引進去
寫上using MySql.Data.MySqlClient;
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using System.Threading.Tasks;
6 using MySql.Data.MySqlClient;
7 namespace ConsoleApplication2
8 {
9 class Program
10 {
11 static void Main(string[] args)
12 {
13
14 //VerifyUSser("root", "123456");
15
16 //InsertSQL();
17 DeleteSQL();
18 // UpdateSQL();
19 ReadSQL();
20 Console.ReadKey();
21 }
22
23 /// <summary>
24 /// 讀取資料庫
25 /// </summary>
26 static void ReadSQL()
27 {
28 string connectStr = "server=127.0.0.1;port=3306;database=test;user=root;password=123456;SslMode=none;charset=utf8";
29 MySqlConnection conn = new MySqlConnection(connectStr);
30 try
31 {
32 conn.Open(); //建立連接配接,打開資料庫
33 Console.WriteLine("打開資料庫成功");
34 string sqlstr = "select * from student";
35 MySqlCommand cmd = new MySqlCommand(sqlstr, conn);
36 MySqlDataReader reader = cmd.ExecuteReader();
37 //reader.Read(); //讀取下一頁資料 ,讀取成功,傳回true,下一頁沒有資料則傳回false表示到了最後一頁
38 while (reader.Read()) //周遊表中資料
39 {
40 int sno = reader.GetInt32(reader.GetOrdinal("sno"));
41 string name = reader.GetString(reader.GetOrdinal("name"));
42 string sex = reader.GetString(reader.GetOrdinal("sex"));
43 int age = reader.GetInt32(reader.GetOrdinal("age"));
44 Console.WriteLine(sno + " " + name + " " + sex + " " + age);
45 }
46 }
47 catch (Exception ex)
48 {
49 Console.WriteLine(ex.ToString());
50 }
51 finally
52 {
53 conn.Close(); //關閉連接配接
54 }
55 }
56
57 static bool VerifyUSser()
58 {
59 string username = "";
60 string password = "";
61 string server = "";
62 string port = "";
63 string database = "";
64 string connectStr = "server=" + server + ";"
65 + "port=" + port + ";"
66 + "database=" + database + ";"
67 + "username=" + username + ";"
68 + "password=" + password + ";"
69 + "SslMode=none" //防止報sll connecttion 錯誤
70 + "charset=utf8"; //可以插入中文
71 MySqlConnection conn = new MySqlConnection(connectStr); //建立資料庫連接配接
72 try
73 {
74 conn.Open(); //建立連接配接,打開資料庫
75 string sqlstr = "select * from student";
76 MySqlCommand cmd = new MySqlCommand(sqlstr, conn);
77 //通過設定參數的形式給SQL 語句串值
78 cmd.Parameters.AddWithValue("para1", username);
79 cmd.Parameters.AddWithValue("para2", password);
80
81 MySqlDataReader reader = cmd.ExecuteReader();
82 if (reader.Read())
83 {
84 return true;
85 }
86 return false;
87 }
88 catch (Exception ex)
89 {
90 Console.WriteLine(ex.ToString());
91 }
92 finally
93 {
94 conn.Close(); //關閉連接配接
95 }
96 return false;
97 }
98
99
100 /// <summary>
101 /// 插入資料到資料庫中
102 /// </summary>
103 static void InsertSQL()
104 {
105 string connectStr = "server=127.0.0.1;port=3306;database=test;user=root;password=123456;SslMode=none;charset=utf8";
106 //并沒有建立資料庫連接配接
107 MySqlConnection conn = new MySqlConnection(connectStr);
108 try
109 { //建立連接配接,打開資料庫
110 conn.Open();
111 //注意一條SQL語句不要一條語句執行兩次,會因為已存在而報錯
112 string sqlstr = "insert into student(sno,name,sex,age) values(1105,\"sdfd\",\"男\",321)"; //SQL語句
113 MySqlCommand cmd = new MySqlCommand(sqlstr, conn);
114 //int result = cmd.ExecuteNonQuery(); //傳回值為執行後資料庫中受影響的資料行數
115 //Console.WriteLine("執行成功,影響了{0}行資料", result);
116 }
117 catch (Exception ex)
118 {
119 Console.WriteLine(ex.ToString());
120 }
121 finally
122 {
123 conn.Close();//關閉連接配接
124 }
125 }
126
127 /// <summary>
128 /// 修改資料庫中資料
129 /// </summary>
130 static void UpdateSQL()
131 {
132 string connectStr = "server=127.0.0.1;port=3306;database=test;user=root;password=123456;SslMode=none;charset=utf8";
133 //并沒有建立資料庫連接配接
134 MySqlConnection conn = new MySqlConnection(connectStr);
135 try
136 { //建立連接配接,打開資料庫
137 conn.Open();
138 string sqlstr = "update student set age=28,name='sdsffcc' where sno=1105"; //SQL語句
139 MySqlCommand cmd = new MySqlCommand(sqlstr, conn);
140 int result = cmd.ExecuteNonQuery(); //傳回值為執行後資料庫中受影響的資料行數
141 Console.WriteLine("執行成功,影響了{0}行資料", result);
142 }
143 catch (Exception ex)
144 {
145 Console.WriteLine(ex.ToString());
146 }
147 finally
148 {
149 conn.Close(); //關閉連接配接
150 }
151 }
152
153 /// <summary>
154 /// 删除資料庫中的資料
155 /// </summary>
156 static void DeleteSQL()
157 {
158 string connectStr = "server=127.0.0.1;port=3306;database=test;user=root;password=123456;SslMode=none;charset=utf8";
159 //并沒有建立資料庫連接配接
160 MySqlConnection conn = new MySqlConnection(connectStr);
161 try
162 { //建立連接配接,打開資料庫
163 conn.Open();
164 //注意一條SQL語句不要一條語句執行兩次,會因為已存在而報錯
165 string sqlstr = "Delete from student where sno = 1105"; //SQL語句
166 MySqlCommand cmd = new MySqlCommand(sqlstr, conn);
167 int result = cmd.ExecuteNonQuery(); //傳回值為執行後資料庫中受影響的資料行數
168 Console.WriteLine("執行成功,影響了{0}行資料", result);
169 }
170 catch (Exception ex)
171 {
172 Console.WriteLine(ex.ToString());
173 }
174 finally
175 {
176 conn.Close(); //關閉連接配接
177 }
178 }
179 }
180 }