首页 > 数据库 > mysql教程 > C#连接mysql (vs2010)

C#连接mysql (vs2010)

WBOY
发布: 2016-06-07 15:46:39
原创
1089 人浏览过

在vs2010中 工具-数据库连接 里要想连接到mysql数据库,需要安装这样一个东西: MySql Connector;1.首先安装 connector 。下载地址:(http://www.mysql.com/downloads/connector/net/)2.现在就可以了,打开vs2010试试吧! 安装完成后找到安装目录下(C:\Pr

在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,需如下code:

<span>//define the connection reference and initialize it</span>
登录后复制
 MySql.<span>Data</span>.<span>MySqlClient</span>.<span>MySqlConnection</span> msqlConnection <span><strong>=</strong></span> <span><strong>null</strong></span><span><strong>;</strong></span>
登录后复制
 msqlConnection <span><strong>=</strong></span> <span><strong>new</strong></span> MySql.<span>Data</span>.<span>MySqlClient</span>.<span>MySqlConnection</span><span><strong>(</strong></span>“<span><strong>server</strong></span><span><strong>=</strong></span>localhost<span><strong>;</strong></span>user id<span><strong>=</strong></span>UserName<span><strong>;</strong></span>Password<span><strong>=</strong></span>UserPassword<span><strong>;</strong></span>database<span><strong>=</strong></span>DatabaseName<span><strong>;</strong></span>persist security info<span><strong>=</strong></span><span><strong>False</strong></span>”<span><strong>)</strong></span><span><strong>;</strong></span>
登录后复制
 <span>//define the command reference</span>
登录后复制
 MySql.<span>Data</span>.<span>MySqlClient</span>.<span>MySqlCommand</span> msqlCommand <span><strong>=</strong></span> <span><strong>new</strong></span> MySql.<span>Data</span>.<span>MySqlClient</span>.<span>MySqlCommand</span><span><strong>(</strong></span><span><strong>)</strong></span><span><strong>;</strong></span>
登录后复制
 <span>//define the connection used by the command object</span>
登录后复制
 msqlCommand.<span>Connection</span> <span><strong>=</strong></span> this.<span>msqlConnection</span><span><strong>;</strong></span>
登录后复制
 <span>//define the command text</span>
登录后复制
 msqlCommand.<span>CommandText</span> <span><strong>=</strong></span> <span>"SELECT * FROM TestTable;"</span><span><strong>;</strong></span>
登录后复制
 try
登录后复制
<span><strong>{</strong></span>
登录后复制
登录后复制
登录后复制
 <span>//open the connection</span>
登录后复制
 this.<span>msqlConnection</span>.<span>Open</span><span><strong>(</strong></span><span><strong>)</strong></span><span><strong>;</strong></span>
登录后复制
 <span>//use a DataReader to process each record</span>
登录后复制
 MySql.<span>Data</span>.<span>MySqlClient</span>.<span>MySqlDataReader</span> msqlReader <span><strong>=</strong></span> msqlCommand.<span>ExecuteReader</span><span><strong>(</strong></span><span><strong>)</strong></span><span><strong>;</strong></span>
登录后复制
<span><strong> while</strong></span> <span><strong>(</strong></span>msqlReader.<span>Read</span><span><strong>(</strong></span><span><strong>)</strong></span><span><strong>)</strong></span>
登录后复制
 <span><strong>{</strong></span>
登录后复制
 <span>//do something with each record</span>
登录后复制
 <span><strong>}</strong></span>
登录后复制
<span><strong>}</strong></span>
登录后复制
登录后复制
登录后复制
catch <span><strong>(</strong></span>Exception er<span><strong>)</strong></span>
登录后复制
<span><strong>{</strong></span>
登录后复制
登录后复制
登录后复制
 <span>//do something with the exception</span>
登录后复制
<span><strong>}</strong></span>
登录后复制
登录后复制
登录后复制
finally
登录后复制
<span><strong>{</strong></span>
登录后复制
登录后复制
登录后复制
<span> //always close the connection</span>
登录后复制
 this.<span>msqlConnection</span>.<span>Close</span><span><strong>(</strong></span><span><strong>)</strong></span><span><strong>;</strong></span>
登录后复制
<span><strong>}</strong></span>
登录后复制
登录后复制
登录后复制
相关标签:
来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板