C#连接MYSQL大数据库地3种方法及示例.docx
- 文档编号:5492654
- 上传时间:2022-12-17
- 格式:DOCX
- 页数:20
- 大小:113.08KB
C#连接MYSQL大数据库地3种方法及示例.docx
《C#连接MYSQL大数据库地3种方法及示例.docx》由会员分享,可在线阅读,更多相关《C#连接MYSQL大数据库地3种方法及示例.docx(20页珍藏版)》请在冰豆网上搜索。
C#连接MYSQL大数据库地3种方法及示例
C#连接MYSQL数据库的方法及示例连接MYSQL数据库的方法及示例方法一:
usingMySql.Data
usingMySql.Data.MySqlClient;
其他操作跟SQL是差不多,无非就是前缀变成MySql了.
补充:
下面是连接字符串,供参考.MySqlConnectioncon=newMySql.Data.MySqlClient.MySqlConnection("Database='testdb';DataSource='localhost';UserId='db';Password='apple';charset='utf8'");con.Open();
MySqlCommandcmd=newMySqlCommand();
cmd.Connection=con;
使用MYSQL推出的MySQLConnector/NetisanADO.NETdriverforMySQL该组件为MYSQL为ADO.NET访问MYSQL数据库设计的.NET访问组件。
安装完成该组件后,引用命名空间MySql.Data.MySqlClient;
使用命令行编译时:
csc/r:
MySql.Data.dlltest.cs方法二:
通过ODBC访问MYSQL数据库
访问前要先下载两个组件:
和MYSQL的ODBC驱动(MySQLConnector/ODBC(MyODBC)driver)目前为3.51版
安装完成后,即可通过ODBC访问MYSQL数据库方法三:
使用CoreLab推出的MYSQL访问组件,面向.NET
安装完成后,引用命名空间:
CoreLab.MySql;
使用命令编译时:
csc/r:
CoreLab.MySql.dlltest.cs
以下为访问MYSQL数据库实例
编译指令:
csc/r:
CoreLab.MySql.dll/r:
MySql.Data.dlltest.cs
usingSystem;
usingSystem.Net;
usingSystem.Text;
usingCoreLab.MySql;
usingSystem.Data.Odbc;
usingMySql.Data.MySqlClient;
classConnectMySql
{
publicvoidConnect_CoreLab()
{
stringconstr="UserId=root;Host=localhost;Database=qing;password=qing";MySqlConnectionmycn=newMySqlConnection(constr);
mycn.Open();
MySqlCommandmycm=newMySqlCommand("select*fromshop",mycn);MySqlDataReadermsdr=mycm.ExecuteReader();
while(msdr.Read())
{
if(msdr.HasRows)
{
Console.WriteLine(msdr.GetString(0));
}
}msdr.Close();mycn.Close();
}
publicvoidConnect_Odbc()
{
//stringMyConString="DSN=MySQL;UID=root;PWD=qing";stringMyConString="DRIVER={MySQLODBC3.51Driver};"+"SERVER=localhost;"+
"DATABASE=test;"+
"UID=root;"+
"PASSWORD=qing;"+"OPTION=3";
OdbcConnectionMyConn=newOdbcConnection(MyConString);MyConn.Open();
OdbcCommandmycm=newOdbcCommand("select*fromhello",MyConn);OdbcDataReadermsdr=mycm.ExecuteReader();
while(msdr.Read())
{
if(msdr.HasRows)
{
Console.WriteLine(msdr.GetString(0));
}
}msdr.Close();
MyConn.Close();
}
publicvoidConnect_Net()
{
stringmyConnectionString="Database=test;DataSource=localhost;UserId=root;Password=qing";
MySqlConnectionmycn=newMySqlConnection(myConnectionString);mycn.Open();
MySqlCommandmycm=newMySqlCommand("select*fromhello",mycn);MySqlDataReadermsdr=mycm.ExecuteReader();
while(msdr.Read())
{
if(msdr.HasRows)
{
Console.WriteLine(msdr.GetString(0));
}
}msdr.Close();
mycn.Close();
}
publicstaticvoidMain()
{
ConnectMySqlms=newConnectMySql();ms.Connect_CoreLab();
ms.Connect_Odbc();
Connect_Net();
}
}
1、用MySQLDriverCS连接MySQL数据库先下载和安装MySQLDriverCS,地址:
在安装文件夹下面找到MySQLDriver.dll,然后将MySQLDriver.dll添加引用到项目中
注:
我下载的是版本是MySQLDriverCS-n-EasyQueryTools-4.0.1-DotNet2.0.exeusingSystem;
usingSystem.Collections.Generic;
usingSystem.ComponentModel;
usingSystem.Data;
usingSystem.Data.Odbc;
usingSystem.Drawing;
usingSystem.Linq;
usingSystem.Text;
usingSystem.Windows.Forms;
usingMySQLDriverCS;
namespacemysql
{
publicpartialclassForm1:
Form
{
publicForm1()
{
InitializeComponent();
}
privatevoidForm1_Load(objectsender,EventArgse)
{
MySQLConnectionconn=null;
conn=newMySQLConnection(newMySQLConnectionString("localhost","inv","root","831025").AsString);
conn.Open();
MySQLCommandcommn=newMySQLCommand("setnamesgb2312",conn);
commn.ExecuteNonQuery();
stringsql="select*fromexchange";
MySQLDataAdaptermda=newMySQLDataAdapter(sql,conn);
DataSetds=newDataSet();
mda.Fill(ds,"table1");
this.dataGrid1.DataSource=ds.Tables["table1"];
conn.Close();
}
2、通过ODBC访问mysql数据库:
参考:
.microsoft./china/community/Column/63.mspx
1.安装MicrosoftODBC.net:
我安装的是mysql-connector-odbc-3.51.22-win32.msi
2.安装MDAC2.7或者更高版本:
我安装的是mdac_typ.exe2.7简体中文版
3.安装MySQL的ODBC驱动程序:
我安装的是odbc_net.msi
4.管理工具->数据源ODBC–>配置DSN⋯
5.解决方案管理中添加引用Microsoft.Data.Odbc.dll(1.0.3300)
6.代码中增加引用usingMicrosoft.Data.Odbc;
usingSystem;
usingSystem.Collections.Generic;
usingSystem.ComponentModel;
usingSystem.Drawing;
usingSystem.Linq;//vs2005好像没有这个命名空间,在c#2008下测试自动生成的
usingSystem.Text;
usingSystem.Windows.Forms;
usingMicrosoft.Data.Odbc;
namespacemysql
publicpartialclassForm1:
Form
{
publicForm1()
{
InitializeComponent();
}
privatevoidForm1_Load(objectsender,EventArgse)
{
stringMyConString="DRIVER={MySQLODBC3.51Driver};"+
"SERVER=localhost;"+
"DATABASE=inv;"+
"UID=root;"+
"PASSWORD=831025;"+
"OPTION=3";
OdbcConnectionMyConnection=newOdbcConnection(MyConString);
MyConnection.Open();
Console.WriteLine("\nsuccess,connectedsuccessfully!
\n");
stringquery="insertintotestvalues(''hello'',''lucas'',''liu'')";
OdbcCommandcmd=newOdbcCommand(query,MyConnection);
//处理异常:
插入重复记录有异常
try{
cmd.ExecuteNonQuery();
}
catch(Exceptionex){
Console.WriteLine("recordduplicate.");
}finally{
cmd.Dispose();
}
//***********************
用read方法读数据到textbox**********************
stringtmp1
=null;
stringtmp2
=null;
stringtmp3
=null;
query="select*fromtest";
OdbcCommandcmd2=newOdbcCommand(query,MyConnection);
OdbcDataReaderreader=cmd2.ExecuteReader();
while(reader.Read())
{
tmp1=
reader[0].ToString();
tmp2=reader[1].ToString();
tmp3=reader[2].ToString();
}
this.textBox1.Text=tmp1+""+tmp2+""+tmp3;
*/
//************************
用datagridview控件显示数据表**************************
stringMyConString="DRIVER={MySQLODBC3.51Driver};"+
"SERVER=localhost;"+"DATABASE=inv;"+
"UID=root;"+"PASSWORD=831025;"+
"OPTION=3";
OdbcConnectionMyConnection=newOdbcConnection(MyConString);
OdbcDataAdapteroda=newOdbcDataAdapter("select*fromcustomer",MyConnection);DataSetds=newDataSet();
oda.Fill(ds,"employee");this.dataGridView1.DataSource=ds.Tables["employee"];
*/
MyConnection.Close();
}
文章出处:
.diybl./course/4_webprogram/1、用MySQLDriverCS连接MySQL数据库
先下载和安装MySQLDriverCS,地址:
在安装文件夹下面找到MySQLDriver.dll,然后将MySQLDriver.dll添加引用到项目中
注:
我下载的是版本是MySQLDriverCS-n-EasyQueryTools-4.0.1-DotNet2.0.exe
usingSystem;
usingSystem.Collections.Generic;
usingSystem.ComponentModel;
usingSystem.Data;
usingSystem.Data.Odbc;
usingSystem.Drawing;
usingSystem.Linq;
usingSystem.Text;
usingSystem.Windows.Forms;
usingMySQLDriverCS;
namespacemysql
publicpartialclassForm1:
Form
{
publicForm1()
{
InitializeComponent();
}
privatevoidForm1_Load(objectsender,EventArgse)
{
MySQLConnectionconn=null;
conn=newMySQLConnection(newMySQLConnectionString("localhost","inv","root","831025").AsString);
conn.Open();
MySQLCommandcommn=newMySQLCommand("setnamesgb2312",conn);
commn.ExecuteNonQuery();
stringsql="select*fromexchange";
MySQLDataAdaptermda=newMySQLDataAdapter(sql,conn);
DataSetds=newDataSet();
mda.Fill(ds,"table1");
this.dataGrid1.DataSource=ds.Tables["table1"];
conn.Close();
2、通过ODBC访问mysql数据库:
参考:
.microsoft./china/community/Column/63.mspx
1.安装MicrosoftODBC.net:
我安装的是mysql-connector-odbc-3.51.22-win32.msi
2.安装MDAC2.7或者更高版本:
我安装的是mdac_typ.exe2.7简体中文版
3.安装MySQL的ODBC驱动程序:
我安装的是odbc_net.msi
4.管理工具->数据源ODBC–>配置DSN⋯
5.解决方案管理中添加引用Microsoft.Data.Odbc.dll(1.0.3300)
6.代码中增加引用usingMicrosoft.Data.Odbc;
usingSystem;
usingSystem.Collections.Generic;
usingSystem.ComponentModel;
usingSystem.Drawing;
usingSystem.Linq;//vs2005好像没有这个命名空间,在c#2008下测试自动生成的
usingSystem.Text;
usingSystem.Windows.Forms;
usingMicrosoft.Data.Odbc;
namespacemysql
{
publicpartialclassForm1:
Form
{
publicForm1()
{
InitializeComponent();
}
privatevoidForm1_Load(objectsender,EventArgse)
{
stringMyConString="DRIVER={MySQLODBC3.51Driver};"+
"SERVER=localhost;"+
"DATABASE=inv;"+"UID=root;"+
"PASSWORD=831025;"+
"OPTION=3";
OdbcConnectionMyConnection=newOdbcConnection(MyConString);
MyConnection.Open();
Console.WriteLine("\nsuccess,connectedsuccessfully!
\n");
stringquery="insertintotestvalues(''hello'',''lucas'',''liu'')";
OdbcCommandcmd=newOdbcCommand(query,MyConnection);
//处理异常:
插入重复记录有异常
try{
cmd.ExecuteNonQuery();
}
catch(Exceptionex){
Console.WriteLine("recordduplicate.");
}finally{
cmd.Dispose();
}
stringtmp1=null;
stringtmp2=null;
stringtmp3=null;
query="select*fromtest";
OdbcCommandcmd2=newOdbcCommand(query,MyConnection);
OdbcDataReaderreader=cmd2.ExecuteReader();
while(reader.Read())
{
tmp1=reader[0].ToString();
tmp2=reader[1].ToString();
tmp3=reader[2].ToString();
}
this.textBox1.Text=tmp1+""+tmp2+""+tmp3;
*/
//************************
用datagridview控件显示数据表**************************
stringMyConString="DRIVER={MySQLODBC3.51Driver};"+
"SERVER=localhost;"+"DATABASE=inv;"+"UID=root;"+
"PASSWORD=831025;"+"OPTION=3";
OdbcConnectionMyConnection=newOdbcConnection(MyConString);
OdbcDataAdapteroda=newOdbcDataAdapter("select*fromcustomer",MyConnection);
DataSetds=newDataSet();
oda.Fill(ds,"employee");
this.dataGridView1.DataSource=ds.Tables["employee"];
*/
MyConnection.Close();
}
}
}
文章出处:
.diybl./course/4_webprogram/
C#连接mysql数据库
1.连接:
1.安装MicrosoftODBC.net。
2.安装MySQL的ODBC驱动程序。
2.解决方案管理中添加引用Microsoft.Data.Odbc.dll(1.0.3300)
3.代码中增加引用
usingMicrosoft.Data.Odbc;
4.编写代码
stringMyConString="DRIVER={MySQLODBC3.51Driver};"+"SERVER=localhost;"+
"DATABASE=samp_db;"+
"UID=root;"+
"PASSWORD=;"+
"OPTION=3";
//ConnecttoMySQLusingConnector/ODBC
MyConnection.Open();
Console.WriteLine("\n!
!
!
success,connectedsuccessfully!
!
!
\n");MyConnection.Close();
2.全部例程:
/**
*sample:
mycon.cs
*purpose:
DemosampleforODB
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- C# 连接 MYSQL 数据库 方法 示例
![提示](https://static.bdocx.com/images/bang_tan.gif)