c#连接MySql数据库的两种方法

1、用MySQLDriverCS连接MySQL数据库

先下载和安装MySQLDriverCS,地址:

http://sourceforge.net/projects/mysqldrivercs/

在安装文件夹下面找到MySQLDriver.dll,然后将MySQLDriver.dll添加引用到项目中

注:我下载的是版本是 MySQLDriverCS-n-EasyQueryTools-4.0.1-DotNet2.0.exe


[java]  view plain copy print ?
  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.    
  12.    
  13. namespace mysql  
  14. {  
  15.     public partial class Form1 : Form  
  16.     {  
  17.         public Form1()  
  18.         {  
  19.             InitializeComponent();  
  20.         }  
  21.    
  22.         private void Form1_Load(object sender, EventArgs e)  
  23.         {  
  24.    
  25.             MySQLConnection conn = null;  
  26.             conn = new MySQLConnection(new MySQLConnectionString("localhost""inv""root""831025").AsString);  
  27.             conn.Open();  
  28.    
  29.             MySQLCommand commn = new MySQLCommand("set names gb2312", conn);  
  30.             commn.ExecuteNonQuery();  
  31.    
  32.             string sql = "select * from exchange ";  
  33.             MySQLDataAdapter mda = new MySQLDataAdapter(sql, conn);  
  34.    
  35.             DataSet ds = new DataSet();  
  36.             mda.Fill(ds, "table1");  
  37.    
  38.             this.dataGrid1.DataSource = ds.Tables["table1"];  
  39.             conn.Close();  
  40.    
  41.         }  
  42.    
  43.     
  44.     }  
  45. }  



2、通过ODBC访问mysql数据库:

 

参考:http://www.microsoft.com/china/community/Column/63.mspx

 

1.      安装Microsoft ODBC.net:我安装的是mysql-connector-odbc-3.51.22-win32.msi

2.      安装MDAC 2.7或者更高版本:我安装的是mdac_typ.exe 2.7简体中文版

3.      安装MySQLODBC驱动程序:我安装的是 odbc_net.msi

4.      管理工具 -> 数据源ODBC –>配置DSN…

5.      解决方案管理中添加引用 Microsoft.Data.Odbc.dll(1.0.3300)

6.      代码中增加引用 using Microsoft.Data.Odbc;



[java]  view plain copy print ?
  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.    
  10. namespace mysql  
  11. {  
  12.     public partial class Form1 : Form  
  13.     {  
  14.         public Form1()  
  15.         {  
  16.             InitializeComponent();  
  17.         }  
  18.    
  19.         private void Form1_Load(object sender, EventArgs e)  
  20.         {  
  21.    
  22.             string MyConString = "DRIVER={MySQL ODBC 3.51 Driver};" +  
  23.                                  "SERVER=localhost;" +  
  24.                                  "DATABASE=inv;" +  
  25.                                  "UID=root;" +  
  26.                                  "PASSWORD=831025;" +  
  27.                                  "OPTION=3";  
  28.             OdbcConnection MyConnection = new OdbcConnection(MyConString);  
  29.             MyConnection.Open();  
  30.             Console.WriteLine(""n success, connected successfully !"n");  
  31.              
  32.             string query = "insert into test values( 'hello', 'lucas', 'liu')";  
  33.             OdbcCommand cmd = new OdbcCommand(query, MyConnection);  
  34.            
  35.             //处理异常:插入重复记录有异常  
  36. try{  
  37.   cmd.ExecuteNonQuery();  
  38. }  
  39. catch(Exception ex){  
  40.                  Console.WriteLine("record duplicate.");  
  41. }finally{  
  42.                  cmd.Dispose();  
  43. }  
  44.    
  45. //***********************用read方法读数据到textbox**********************  
  46.             string tmp1 = null;  
  47.             string tmp2 = null;  
  48.             string tmp3 = null;  
  49.             query = "select * from test ";  
  50.             OdbcCommand cmd2 = new OdbcCommand(query, MyConnection);  
  51.             OdbcDataReader reader = cmd2.ExecuteReader();  
  52.             while (reader.Read())  
  53.             {  
  54.                 tmp1 = reader[0].ToString();  
  55.                 tmp2 = reader[1].ToString();  
  56.                 tmp3 = reader[2].ToString();  
  57.             }  
  58.             this.textBox1.Text = tmp1 + " " + tmp2 + " " + tmp3;  
  59.             */  
  60.    
  61. //************************用datagridview控件显示数据表**************************  
  62. string MyConString = "DRIVER={MySQL ODBC 3.51 Driver};" +  
  63.                                  "SERVER=localhost;" +  
  64.                                  "DATABASE=inv;" +  
  65.                                  "UID=root;" +  
  66.                                  "PASSWORD=831025;" +  
  67.                                  "OPTION=3";  
  68.           OdbcConnection MyConnection = new OdbcConnection(MyConString);  
  69. OdbcDataAdapter oda = new OdbcDataAdapter("select * from customer ", MyConnection);  
  70. DataSet ds = new DataSet();  
  71.   
  72.           oda.Fill(ds, "employee");  
  73.           this.dataGridView1.DataSource = ds.Tables["employee"];  
  74. */  
  75.    
  76.            MyConnection.Close();  
  77.         }  
  78.     }  
  79. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值