c# 连接MySql数据库的两种方法
2010-04-15 20:40
513 查看
1、用MySQLDriverCS连接MySQL数据库
先下载和安装MySQLDriverCS,地
址:
http://sourceforge.net/projects/mysqldrivercs/
在安装文件夹下面找到
MySQLDriver.dll
,然后将
MySQLDriver.dll
添加引用到项目中
注:我下载的是版本是
MySQLDriverCS-n-EasyQueryTools-4.0.1-DotNet2.0.exe
using
System;
using
System.Collections.Generic;
using
System.ComponentModel;
using
System.Data;
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
{
public
Form1()
{
InitializeComponent();
}
private
void
Form1_Load(object
sender, EventArgs
e)
{
MySQLConnection
conn = null
;
conn = new
MySQLConnection
(new
MySQLConnectionString
("localhost"
, "inv"
, "root"
, "831025"
).AsString);
conn.Open();
MySQLCommand
commn = new
MySQLCommand
("set names gb2312"
, conn);
commn.ExecuteNonQuery();
string
sql = "select * from exchange "
;
MySQLDataAdapter
mda = new
MySQLDataAdapter
(sql,
conn);
DataSet
ds = new
DataSet
();
mda.Fill(ds, "table1"
);
this
.dataGrid1.DataSource =
ds.Tables["table1"
];
conn.Close();
}
}
}
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.
安装
MySQL
的
ODBC
驱动程
序:我安装的是
odbc_net.msi
4.
管理工具
->
数据源
ODBC –>
配置
DSN…
5.
解决方案管理中添加引用
Microsoft.Data.Odbc.dll(1.0.3300)
6.
代码中增加引用
using Microsoft.Data.Odbc;
using
System;
using
System.Collections.Generic;
using
System.ComponentModel;
using
System.Drawing;
using
System.Linq; //vs2005
好像没
有这个命名空间,在c#2008下测试自动生成的
using
System.Text;
using
System.Windows.Forms;
using
Microsoft.Data.Odbc;
namespace
mysql
{
public
partial
class
Form1
: Form
{
public
Form1()
{
InitializeComponent();
}
private
void
Form1_Load(object
sender, EventArgs
e)
{
string
MyConString = "DRIVER={MySQL ODBC 3.51 Driver};"
+
"SERVER=localhost;"
+
"DATABASE=inv;"
+
"UID=root;"
+
"PASSWORD=831025;"
+
"OPTION=3"
;
OdbcConnection
MyConnection = new
OdbcConnection
(MyConString);
MyConnection.Open();
Console
.WriteLine(""n success, connected successfully
!"n"
);
string
query = "insert into test values( 'hello', 'lucas', 'liu')"
;
OdbcCommand
cmd = new
OdbcCommand
(query,
MyConnection);
//
处理异
常:插入重复记录有异常
try{
cmd.ExecuteNonQuery();
}
catch(Exception ex){
Console
.WriteLine("record duplicate."
);
}finally{
cmd.Dispose();
}
//***********************
用
read方法读数据到textbox**********************
string
tmp1 = null
;
string
tmp2 = null
;
string
tmp3 = null
;
query = "select * from test "
;
OdbcCommand
cmd2 = new
OdbcCommand
(query,
MyConnection);
OdbcDataReader
reader =
cmd2.ExecuteReader();
while
(reader.Read())
{
tmp1 = reader[0].ToString();
tmp2 = reader[1].ToString();
tmp3 = reader[2].ToString();
}
this
.textBox1.Text = tmp1 + " "
+ tmp2 + " "
+ tmp3;
*/
//************************
用
datagridview控件显示数据表**************************
string
MyConString = "DRIVER={MySQL ODBC 3.51 Driver};"
+
"SERVER=localhost;"
+
"DATABASE=inv;"
+
"UID=root;"
+
"PASSWORD=831025;"
+
"OPTION=3"
;
OdbcConnection
MyConnection = new
OdbcConnection
(MyConString);
OdbcDataAdapter oda = new OdbcDataAdapter("select *
from customer ", MyConnection);
DataSet ds = new DataSet();
oda.Fill(ds, "employee");
this.dataGridView1.DataSource =
ds.Tables["employee"];
*/
MyConnection.Close();
}
}
}
文章转自: http://www.cnblogs.com/xiaohaikong/archive/2009/08/12/1544478.html
先下载和安装MySQLDriverCS,地
址:
http://sourceforge.net/projects/mysqldrivercs/
在安装文件夹下面找到
MySQLDriver.dll
,然后将
MySQLDriver.dll
添加引用到项目中
注:我下载的是版本是
MySQLDriverCS-n-EasyQueryTools-4.0.1-DotNet2.0.exe
using
System;
using
System.Collections.Generic;
using
System.ComponentModel;
using
System.Data;
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
{
public
Form1()
{
InitializeComponent();
}
private
void
Form1_Load(object
sender, EventArgs
e)
{
MySQLConnection
conn = null
;
conn = new
MySQLConnection
(new
MySQLConnectionString
("localhost"
, "inv"
, "root"
, "831025"
).AsString);
conn.Open();
MySQLCommand
commn = new
MySQLCommand
("set names gb2312"
, conn);
commn.ExecuteNonQuery();
string
sql = "select * from exchange "
;
MySQLDataAdapter
mda = new
MySQLDataAdapter
(sql,
conn);
DataSet
ds = new
DataSet
();
mda.Fill(ds, "table1"
);
this
.dataGrid1.DataSource =
ds.Tables["table1"
];
conn.Close();
}
}
}
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.
安装
MySQL
的
ODBC
驱动程
序:我安装的是
odbc_net.msi
4.
管理工具
->
数据源
ODBC –>
配置
DSN…
5.
解决方案管理中添加引用
Microsoft.Data.Odbc.dll(1.0.3300)
6.
代码中增加引用
using Microsoft.Data.Odbc;
using
System;
using
System.Collections.Generic;
using
System.ComponentModel;
using
System.Drawing;
using
System.Linq; //vs2005
好像没
有这个命名空间,在c#2008下测试自动生成的
using
System.Text;
using
System.Windows.Forms;
using
Microsoft.Data.Odbc;
namespace
mysql
{
public
partial
class
Form1
: Form
{
public
Form1()
{
InitializeComponent();
}
private
void
Form1_Load(object
sender, EventArgs
e)
{
string
MyConString = "DRIVER={MySQL ODBC 3.51 Driver};"
+
"SERVER=localhost;"
+
"DATABASE=inv;"
+
"UID=root;"
+
"PASSWORD=831025;"
+
"OPTION=3"
;
OdbcConnection
MyConnection = new
OdbcConnection
(MyConString);
MyConnection.Open();
Console
.WriteLine(""n success, connected successfully
!"n"
);
string
query = "insert into test values( 'hello', 'lucas', 'liu')"
;
OdbcCommand
cmd = new
OdbcCommand
(query,
MyConnection);
//
处理异
常:插入重复记录有异常
try{
cmd.ExecuteNonQuery();
}
catch(Exception ex){
Console
.WriteLine("record duplicate."
);
}finally{
cmd.Dispose();
}
//***********************
用
read方法读数据到textbox**********************
string
tmp1 = null
;
string
tmp2 = null
;
string
tmp3 = null
;
query = "select * from test "
;
OdbcCommand
cmd2 = new
OdbcCommand
(query,
MyConnection);
OdbcDataReader
reader =
cmd2.ExecuteReader();
while
(reader.Read())
{
tmp1 = reader[0].ToString();
tmp2 = reader[1].ToString();
tmp3 = reader[2].ToString();
}
this
.textBox1.Text = tmp1 + " "
+ tmp2 + " "
+ tmp3;
*/
//************************
用
datagridview控件显示数据表**************************
string
MyConString = "DRIVER={MySQL ODBC 3.51 Driver};"
+
"SERVER=localhost;"
+
"DATABASE=inv;"
+
"UID=root;"
+
"PASSWORD=831025;"
+
"OPTION=3"
;
OdbcConnection
MyConnection = new
OdbcConnection
(MyConString);
OdbcDataAdapter oda = new OdbcDataAdapter("select *
from customer ", MyConnection);
DataSet ds = new DataSet();
oda.Fill(ds, "employee");
this.dataGridView1.DataSource =
ds.Tables["employee"];
*/
MyConnection.Close();
}
}
}
文章转自: http://www.cnblogs.com/xiaohaikong/archive/2009/08/12/1544478.html
相关文章推荐
- c#连接MYSQL数据库的两种方法
- c#连接MySql数据库的两种方法
- C# .net连接mysql数据库的两种方法[转]
- c#连接MySql数据库的两种方法
- c#连接MySql数据库的两种方法
- c#连接MySql数据库的两种方法
- c#连接MySql数据库的两种方法
- c#连接MySql数据库的两种方法
- c#连接MySql数据库的两种方法
- c#连接MySql数据库的两种方法
- c#连接MySql数据库的两种方法
- c#连接MySql数据库的两种方法
- c#连接MySql数据库的两种方法
- C#连接mysql数据库主要有两种方法
- c#连接MySql数据库的两种方法
- C# .net连接mysql数据库的两种方法
- c#连接MySql数据库的两种方法
- C# .net连接mysql数据库的两种方法
- c#连接MySql数据库的两种方法
- c#连接MySql数据库的两种方法