当前位置:
文档之家› C#-连接MYSQL数据库的3种方法及示例
C#-连接MYSQL数据库的3种方法及示例
{
//string MyConString ="DSN=MySQL;UID=root;PWD=qing";
string MyConString = "DRIVER={MySQL ODBC 3.51 Driver};" +
"SERVER=localhost;" +
"DATABASE=test;" +
"UID=root;" +
//处理异常:插入重复记录有异常
try{
cmd.ExecuteNonQuery();
}
catch(Exception ex){
Console.WriteLine("record duplicate.");
}finally{
cmd.Dispose();
}
//***********************用read方法读数据到textbox**********************
{
string constr = "User Id=root;Host=localhost;Database=qing;password=qing";
MySqlConnection mycn = new MySqlConnection(constr);
mycn.Open();
MySqlCommand mycm = new MySqlCommand("select * from shop",mycn);
Console.WriteLine("\n success, connected successfully !\n");
string query = "insert into test values( ''hello'', ''lucas'', ''liu'')";
OdbcCommand cmd = new OdbcCommand(query, MyConnection);
{
Console.WriteLine(msdr.GetString(0));
}
}
msdr.Close();
mycn.Close();
}
public static void Main()
{
ConnectMySql ms = new ConnectMySql();
ms.Connect_CoreLab());
}
}
1、用MySQLDriverCS连接MySQL数据库
先下载和安装MySQLDriverCS,地址:
/projects/mysqldrivercs/
在安装文件夹下面找到MySQLDriver.dll,然后将MySQLDriver.dll添加引用到项目中
con.Open();
MySqlCommand cmd = new MySqlCommand();
cmd.Connection = con;
使用MYSQL推出的MySQL Connector/Net is an driver for MySQL
该组件为MYSQL为访问MYSQL数据库设计的.NET访问组件。
安装完成该组件后,引用命名空间MySql.Data.MySqlClient;
使用命令行编译时:csc /r:MySql.Data.dll test.cs
方法二:
通过ODBC访问MYSQL数据库
访问前要先下载两个组件:和MYSQL的ODBC驱动(MySQL Connector/ODBC (MyODBC) driver)目前为3.51版
*/
//************************用datagridview控件显示数据表**************************
注:我下载的是版本是MySQLDriverCS-n-EasyQueryTools-4.0.1-DotNet2.0.exe
using System;
using System.Collections.Generic;
using ponentModel;
using System.Data;
OdbcDataReader msdr = mycm.ExecuteReader();
while(msdr.Read())
{
if (msdr.HasRows)
{
Console.WriteLine(msdr.GetString(0));
}
}
msdr.Close();
MyConn.Close();
}
public void Connect_Net()
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
MySQLConnection conn = null;
conn = new MySQLConnection(new MySQLConnectionString("localhost", "inv", "root", "831025").AsString);
while (reader.Read())
{
tmp1 = reader[0].ToString();
tmp2 = reader[1].ToString();
tmp3 = reader[2].ToString();
}
this.textBox1.Text = tmp1 + " " + tmp2 + " " + tmp3;
DataSet ds = new DataSet();
mda.Fill(ds, "table1");
this.dataGrid1.DataSource = ds.Tables["table1"];
conn.Close();
}
}
}
2、通过ODBC访问mysql数据库:
参考:/china/community/Column/63.mspx
安装完成后,即可通过ODBC访问MYSQL数据库
方法三:
使用CoreLab推出的MYSQL访问组件,面向.NET
安装完成后,引用命名空间:CoreLab.MySql;
使用命令编译时:csc /r:CoreLab.MySql.dll test.cs
以下为访问MYSQL数据库实例
编译指令:csc /r:CoreLab.MySql.dll /r:MySql.Data.dll test.cs
mycn.Open();
MySqlCommand mycm = new MySqlCommand("select * from hello",mycn);
MySqlDataReader msdr = mycm.ExecuteReader();
while(msdr.Read())
{
if (msdr.HasRows)
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
string MyConString = "DRIVER={MySQL ODBC 3.51 Driver};" +
using System.Data.Odbc;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MySQLDriverCS;
namespace mysql
{
public partial class Form1 : Form
{
string myConnectionString = "Database=test;Data Source=localhost;User Id=root;Password=qing";
MySqlConnection mycn = new MySqlConnection(myConnectionString);
5.解决方案管理中添加引用Microsoft.Data.Odbc.dll(1.0.3300)
6.代码中增加引用using Microsoft.Data.Odbc;
using System;
using System.Collections.Generic;
using ponentModel;
MySqlDataReader msdr = mycm.ExecuteReader();
while(msdr.Read())
{
if (msdr.HasRows)
{
Console.WriteLine(msdr.GetString(0));
}
}
msdr.Close();
mycn.Close();
}
public void Connect_Odbc()
using System.Drawing;
using System.Linq; //vs2005好像没有这个命名空间,在c#2008下测试自动生成的