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

C#连接mysql的方法【基于vs2010】,第1张

概述本文实例讲述了C#连接mysql的方法。分享给大家供大家参考,具体如下:在vs2010中 工具->数据库连接  里要想连接到MySQL数据库,需要安装这样一个东西:MySqlConnector;

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

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

1.首先安装 connector 。下载地址:(http://www.mysql.com/downloads/connector/net/)

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 itmysql.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 referencemysql.data.MysqLClIEnt.MysqLCommand msqlCommand = new mysql.data.MysqLClIEnt.MysqLCommand();//define the connection used by the command objectmsqlCommand.Connection = this.msqlConnection;//define the command textmsqlCommand.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#程序设计有所帮助。

总结

以上是内存溢出为你收集整理的C#连接mysql的方法【基于vs2010】全部内容,希望文章能够帮你解决C#连接mysql的方法【基于vs2010】所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1255987.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-07
下一篇 2022-06-07

发表评论

登录后才能评论

评论列表(0条)

保存