当前位置: 移动技术网 > IT编程>开发语言>c# > C#连接mysql的方法【基于vs2010】

C#连接mysql的方法【基于vs2010】

2019年07月18日  | 移动技术网IT编程  | 我要评论

本文实例讲述了c#连接mysql的方法。分享给大家供大家参考,具体如下:

在vs2010中  工具->数据库连接   里要想连接到mysql数据库,需要安装这样一个东西: mysql connector;

1.首先安装 connector 。下载地址:()

2.现在就可以了,打开vs2010试试吧!

安装完成后找到安装目录下(c:\program files\mysql\mysql connector net 6.4.4\assemblies\v4.0\mysql.data.dll,添加引用到工程中.

现在一切就ok了!

如果想在工程c#中连接mysql,需如下代码:

//define the connection reference and initialize it
mysql.data.mysqlclient.mysqlconnection msqlconnection = null;
msqlconnection = new mysql.data.mysqlclient.mysqlconnection("server=localhost;user id=username;password=userpassword;database=databasename;persist security info=false");
//define the command reference
mysql.data.mysqlclient.mysqlcommand msqlcommand = new mysql.data.mysqlclient.mysqlcommand();
//define the connection used by the command object
msqlcommand.connection = this.msqlconnection;
//define the command text
msqlcommand.commandtext = "select * from testtable;";
try
{
 //open the connection
 this.msqlconnection.open();
 //use a datareader to process each record
 mysql.data.mysqlclient.mysqldatareader msqlreader = msqlcommand.executereader();
 while (msqlreader.read())
 {
 //do something with each record
 }
}
catch (exception er)
{
 //do something with the exception
}
finally
{
 //always close the connection
 this.msqlconnection.close();
}

更多关于c#相关内容感兴趣的读者可查看本站专题:《c#程序设计之线程使用技巧总结》、《c#操作excel技巧总结》、《c#中xml文件操作技巧汇总》、《c#常见控件用法教程》、《winform控件用法总结》、《c#数据结构与算法教程》、《c#数组操作技巧总结》及《c#面向对象程序设计入门教程

希望本文所述对大家c#程序设计有所帮助。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网