C#连接mysql数据库主要有两种方法
1、 用MySQL DriverCS连接MySQL数据库
2、 通过ODBC连接MYSQL数据库
具体分析:
1、 使用MYSQL DriverCS 。
首先下载和安装Mysql DriverCS 地址:http://sourceforge.net/projects/mysqldrivercs
在安装文件夹下面找到MySQLDriver.dll,然后将它添加引用到项目中
一般情况下使用:MySQLDriverCS-n-EasyQueryTools-4.0.1-DotNet2.0.exe
下面是连接mysql代码:
1. using System;
2. using System.Collections.Generic;
3. using System.ComponentModel;
4. using System.Data;
5. using System.Data.Odbc;
6. using System.Drawing;
7. using System.Linq;
8. using System.Text;
9. using System.Windows.Forms;
10. using MySQLDriverCS;
11. namespace mysql
12. {
13. public partial class Form1 : Form
14. {
15. public Form1()
16. {
17. InitializeComponent();
18. }
19. private void Form1_Load(object sender, EventArgs e)
20. {
21. MySQLConnection conn = null;
22. conn = new MySQLConnection(new MySQLConnectionString("localhost", "inv", "root", "831025").AsString);
23. conn.Open();
24. MySQLCommand commn = new MySQLCommand("set names gb2312", conn);
25. commn.ExecuteNonQuery();
26. string sql = "select * from exchange ";
27. MySQLDataAdapter mda = new MySQLDataAdapter(sql, conn);
28. DataSet ds = new DataSet();
29. mda.Fill(ds, "table1");
30. this.dataGrid1.DataSource = ds.Tables["table1"];
31. conn.Close();
32. }
33. }
34. }
2、 使用ODBC连接mysql
1. 安装Microsoft ODBC.net:我安装的是mysql-connector-odbc-3.51.22-win32.msi
2. 安装MDAC 2.7或者更高版本:我安装的是mdac_typ.exe 2.7简体中文版
3. 安装MySQL的ODBC驱动程序:我安装的是 odbc_net.msi
4. 管理工具 -> 数据源ODBC –>配置DSN…
5. 解决方案管理中添加引用 Microsoft.Data.Odbc.dll(1.0.3300)
6. 代码中增加引用 using Microsoft.Data.Odbc;
1. using System;
2. using System.Collections.Generic;
3. using System.ComponentModel;
4. using System.Drawing;
5. using System.Linq; //vs2005好像没有这个命名空间,在c#2008下测试自动生成的
6. using System.Text;
7. using System.Windows.Forms;
8. using Microsoft.Data.Odbc;
9. namespace mysql
10. {
11. public partial class Form1 : Form
12. {
13. public Form1()
14. {
15. InitializeComponent();
16. }
17. private void Form1_Load(object sender, EventArgs e)
18. {
19. string MyConString = "DRIVER={MySQL ODBC 3.51 Driver};" +
20. "SERVER=localhost;" +
21. "DATABASE=inv;" +
22. "UID=root;" +
23. "PASSWORD=831025;" +
24. "OPTION=3";
25. OdbcConnection MyConnection = new OdbcConnection(MyConString);
26. MyConnection.Open();
27. Console.WriteLine(""n success, connected successfully !"n");
28. string query = "insert into test values( 'hello', 'lucas', 'liu')";
29. OdbcCommand cmd = new OdbcCommand(query, MyConnection);
30. //处理异常:插入重复记录有异常
31. try{
32. cmd.ExecuteNonQuery();
33. }
34. catch(Exception ex){
35. Console.WriteLine("record duplicate.");
36. }finally{
37. cmd.Dispose();
38. }
39. //***********************用read方法读数据到textbox**********************
40. string tmp1 = null;
41. string tmp2 = null;
42. string tmp3 = null;
43. query = "select * from test ";
44. OdbcCommand cmd2 = new OdbcCommand(query, MyConnection);
45. OdbcDataReader reader = cmd2.ExecuteReader();
46. while (reader.Read())
47. {
48. tmp1 = reader[0].ToString();
49. tmp2 = reader[1].ToString();
50. tmp3 = reader[2].ToString();
51. }
52. this.textBox1.Text = tmp1 + " " + tmp2 + " " + tmp3;
53. */
54. //************************用datagridview控件显示数据表**************************
55. string MyConString = "DRIVER={MySQL ODBC 3.51 Driver};" +
56. "SERVER=localhost;" +
57. "DATABASE=inv;" +
58. "UID=root;" +
59. "PASSWORD=831025;" +
60. "OPTION=3";
61. OdbcConnection MyConnection = new OdbcConnection(MyConString);
62. OdbcDataAdapter oda = new OdbcDataAdapter("select * from customer ", MyConnection);
63. DataSet ds = new DataSet();
64. oda.Fill(ds, "employee");
65. this.dataGridView1.DataSource = ds.Tables["employee"];
66. */
67. MyConnection.Close();
68. }
69. }
70. }