1、);con.Open();MySqlCommand cmd = new MySqlCommand();cmd.Connection = con; 使用MYSQL推出的MySQL Connector/Net is an ADO.NET driver for MySQL 该组件为MYSQL为ADO.NET访问MYSQL数据库设计的.NET访问组件。安装完成该组件后,引用命名空间MySql.Data.MySqlClient;使用命令行编译时:csc /r:MySql.Data.dll test.cs 方法二:通过ODBC访问MYSQL数据库 访问前要先下载两个组件:和MYSQL的ODBC驱动(MyS
2、QL Connector/ODBC (MyODBC) driver)目前为3.51版 安装完成后,即可通过ODBC访问MYSQL数据库 方法三:使用CoreLab推出的MYSQL访问组件,面向.NET 安装完成后,引用命名空间:CoreLab.MySql;使用命令编译时:CoreLab.MySql.dll test.cs 以下为访问MYSQL数据库实例 编译指令:CoreLab.MySql.dll /r:using System;using System.Net;using System.Text;using CoreLab.MySql;using System.Data.Odbc;class
3、 ConnectMySql public void Connect_CoreLab() string constr = User Id=root;Host=localhost;Database=qing;password=qingMySqlConnection mycn = new MySqlConnection(constr);mycn.Open();MySqlCommand mycm = new MySqlCommand(select * from shop,mycn);MySqlDataReader msdr = mycm.ExecuteReader();while(msdr.Read(
4、) if (msdr.HasRows) Console.WriteLine(msdr.GetString(0); msdr.Close();mycn.Close();public void Connect_Odbc() /string MyConString =DSN=MySQL;UID=root;PWD=qingstring MyConString = DRIVER=MySQL ODBC 3.51 Driver; + SERVER=localhost;DATABASE=test;PASSWORD=qing;OPTION=3OdbcConnection MyConn = new OdbcCon
5、nection(MyConString);MyConn.Open();OdbcCommand mycm = new OdbcCommand(select * from hello,MyConn);OdbcDataReader msdr = mycm.ExecuteReader();MyConn.Close();public void Connect_Net() string myConnectionString = Database=test;Data Source=localhost;Password=qingMySqlConnection mycn = new MySqlConnectio
6、n(myConnectionString);public static void Main() ConnectMySql ms = new ConnectMySql();ms.Connect_CoreLab();ms.Connect_Odbc();Connect_Net();1、用MySQLDriverCS连接MySQL数据库先下载和安装MySQLDriverCS,地址:在安装文件夹下面找到MySQLDriver.dll,然后将MySQLDriver.dll添加引用到项目中注:我下载的是版本是 MySQLDriverCS-n-EasyQueryTools-4.0.1-DotNet2.0.exe
7、using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Windows.Forms;using MySQLDriverCS;namespace mysql public partial class Form1 : Form public Form1() InitializeComponent(); private void Form1_Load(object sender, EventArg
8、s e) MySQLConnection conn = null; conn = new MySQLConnection(new MySQLConnectionString(localhost, invroot831025).AsString); conn.Open(); MySQLCommand commn = new MySQLCommand(set names gb2312, conn); commn.ExecuteNonQuery(); string sql = select * from exchange MySQLDataAdapter mda = new MySQLDataAda
9、pter(sql, conn); DataSet ds = new DataSet(); mda.Fill(ds, table1 this.dataGrid1.DataSource = ds.Tables; conn.Close();2、通过ODBC访问mysql数据库:参考:1. 安装Microsoft ODBC.net:我安装的是mysql-connector-odbc-3.51.22-win32.msi2. 安装MDAC 2.7或者更高版本:我安装的是mdac_typ.exe 2.7简体中文版3. 安装MySQL的ODBC驱动程序:我安装的是 odbc_net.msi4. 管理工具 -
10、数据源ODBC 配置DSN5. 解决方案管理中添加引用 Microsoft.Data.Odbc.dll(1.0.3300)6. 代码中增加引用 using Microsoft.Data.Odbc; /vs2005好像没有这个命名空间,在c#2008下测试自动生成的using Microsoft.Data.Odbc; string MyConString = + DATABASE=inv;PASSWORD=831025; OdbcConnection MyConnection = new OdbcConnection(MyConString); MyConnection.Open(); Cons
11、ole.WriteLine(n success, connected successfully !n string query = insert into test values( hello, lucasliu) OdbcCommand cmd = new OdbcCommand(query, MyConnection); /处理异常:插入重复记录有异常try cmd.ExecuteNonQuery();catch(Exception ex)record duplicate.finally cmd.Dispose();/*用read方法读数据到textbox* string tmp1 = n
12、ull; string tmp2 = null; string tmp3 = null; query = select * from test OdbcCommand cmd2 = new OdbcCommand(query, MyConnection); OdbcDataReader reader = cmd2.ExecuteReader(); while (reader.Read() tmp1 = reader0.ToString(); tmp2 = reader1.ToString(); tmp3 = reader2.ToString(); this.textBox1.Text = tm
13、p1 + + tmp2 + + tmp3; */*用datagridview控件显示数据表*OdbcDataAdapter oda = new OdbcDataAdapter(select * from customer , MyConnection);DataSet ds = new DataSet(); oda.Fill(ds, employee this.dataGridView1.DataSource = ds.Tables*/ MyConnection.Close();文章出处:C#连接mysql数据库 1.连接:1.安装Microsoft ODBC.net。2.安装MySQL的ODBC驱动程序。2.解决方案管理中添加引用Microsoft.Data.Odbc.dll(1.0.3300)3.代码中增加引用4.编写代码DATABASE=samp_db;PASSWORD=;/Connect to MySQL using Connector/ODBCOdbcConnection MyConnection = new OdbcConnection(MyConString);MyConnection.Open();Console.WriteL
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1